0

I am using my server code on a raspberry pi and my client code on my laptop. I also off the firewall on my computer. After connecting to the server, I manage to run the loop for once from the client side by keying the word "data" and when I keyed in another command it just came out of the loop. If i key in Quit it says that it have an OS error98 address already in used. May I know how to keep the loop on going ? Below I is my client.py and server.py code.

Server.py code:

import socket
import numpy as np
import encodings

HOST = '192.168.1.65'  
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)
def random_data():          # ANY DATA YOU WANT TO SEND WRITE YOUR SENSOR CODE HERE

    x1 = np.random.randint(0, 55, None)         # Dummy temperature
    y1 = np.random.randint(0, 45, None)         # Dummy humidigy
    my_sensor = "{},{}".format(x1,y1)
    return my_sensor                            # return data seperated by comma
def my_server():

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Server Started waiting for client to connect ")
        s.bind((HOST, PORT))
        s.listen(5)
        conn, addr = s.accept()

        with conn:
            print('Connected by', addr)
            while True:

                data = conn.recv(1024).decode('utf-8')

                if str(data) == "Data":

                    print("Ok Sending data ")

                    my_data = random_data()

                    x_encoded_data = my_data.encode('utf-8')

                    conn.sendall(x_encoded_data)

                elif  str(data) == "Quit":
                    print("shutting down server ")
                    break

                else:
                    pass


if __name__ == '__main__':
    while 1:
        my_server()

Client.py Code:

import socket
import threading
import time


HOST = '192.168.1.65'  # The server's hostname or IP address
PORT = 65432       # The port used by the server


def process_data_from_server(x):
    x1, y1 = x.split(",")
    return x1,y1

def my_client():
    threading.Timer(11, my_client).start()

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))

        my = input("Enter command ")

        my_inp = my.encode('utf-8')

        s.sendall(my_inp)

        data = s.recv(1024).decode('utf-8')

        x_temperature,y_humidity = process_data_from_server(data)

        print("Temperature {}".format(x_temperature))
        print("Humidity {}".format(y_humidity))

        s.close()
        time.sleep(5)


if __name__ == "__main__":
    while 1:
        my_client()
2
  • 1
    if it's not "DATA" try printing what you do get, regardless. Might help debug. Commented Sep 28, 2021 at 10:09
  • I tried other input and it shows me an error s.bind((HOST, PORT)) OSError: [Errno 98] Address already in use Commented Sep 28, 2021 at 10:28

1 Answer 1

1
    address already used

you need to use socket.setsockopt to set socket.SO_REUSEADDR in i think both client and server.py

 def my_server():
    
        # with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        print("Server Started waiting for client to connect ")
        s.bind((HOST, PORT))
        s.listen(5)
        conn, addr = s.accept()
    
        with conn:
            print('Connected by', addr)
            while True:
    
                data = conn.recv(1024).decode('utf-8')
                if str(data) == "Data":
                    ...
                    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.