Home > Software design >  WinError 10057 - Python TCP Server is not receiving client connections from localhost
WinError 10057 - Python TCP Server is not receiving client connections from localhost

Time:11-06

I have this python server code here, which is waiting to receive a message digest and an encrypted message from a python client.

Clientside socket:

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

Server Side:

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
     s.bind((HOST, PORT))
     s.listen()
     conn, addr = s.accept()
     print("\n Server is listing on port :", PORT, "\n")
     fragments = []
    #Execution stops here
     with conn:
        print(f"Connected by {addr}")
        while True:
            chunk = s.recv(4096)
            if not chunk:
             break
        fragments.append(chunk)
        arr = 'b'.join(fragments)
        #Test to see if the array was being populated
        print(arr[0])

I have tried the methods this stackOF post here, specifically above is the provided list method implementation as my client is sending a "packet" of information as a list encoded as a string

packet = [signeddigest, ciphertext]
        transmit = str(packet)
        transmit = transmit.encode()
        s.sendall(transmit)

I have tested my client code on a different server codebase with the same localhost and port number, and that server was receiving the information, so there's something I'm missing in the server side.

The output from the test server was

File [b'HT\xb0\x00~f\xde\xc8G)\xaf*\xcc\x90\xac\xca\x124\x7f\xa0\xaa\ requested from ('127.0.0.1', 49817)

That "file" is the encoded string sent from my client to the test server. So I'm confident there's something wrong with my server implementation.

Further information: When I run the server it listens, then I run the client.

python ClientTest.py Please enter the message to send

Then the server side immediately closes the connection

line 23, in chunk = s.recv(4096) OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

CodePudding user response:

You have a number of inconsistencies in your code:

    while True:
        chunk = s.recv(4096)   # should be conn.recv(4096)
        if not chunk:
         break
    fragments.append(chunk)    # misaligned: you only append the empty chunk
    arr = 'b'.join(fragments)  # 'b' is an unicode string. You want b''

After fixing that to:

    while True:
        chunk = conn.recv(4096)
        if not chunk:
         break
        fragments.append(chunk)
    arr = b''.join(fragments)

arr will receive the sent data as soon as the client uses close or shutdown on its side of the socket.

CodePudding user response:

 s.bind((HOST, PORT))
 s.listen()
 conn, addr = s.accept()

conn is the connected socket, s is the listener socket.

        chunk = s.recv(4096)

The error you get is because you are trying to read from the listener socket s, not from the connected socket conn:

line 23, in chunk = s.recv(4096) ... A request to send or receive data was disallowed because the socket is not connected ...

  • Related