WinError 10057 – Python TCP Server is not receiving client connections from localhost

Question:

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'HTxb0x00~fxdexc8G)xaf*xccx90xacxcax124x7fxa0xaa 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

Asked By: user460178

||

Answers:

 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

Answered By: Steffen Ullrich

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.

Answered By: Serge Ballesta
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.