0

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

  • Check out your firewall settings. Maybe there are some rules blocking connection – mlokos Nov 05 '22 at 16:28
  • thank you, but it's not a firewall issue, I ran the same IP and Port on a different server code base and it received the client information. – user460178 Nov 05 '22 at 16:33
  • @user460178: I'm confused about this question. You claim that it run *"on a different server code base"* - so what changed? What do you mean with *"server code base"* - the code snippet you are showing in your question? Then what is different to what worked and what did not work? And what is the client you refer to? It makes no sense to provide only error messages from the client but not anything about the client (like the code) itself. – Steffen Ullrich Nov 05 '22 at 16:39
  • Maybe there is other process occupying that port. Try to use a different one – mlokos Nov 05 '22 at 16:41
  • Please rework your question so that it is clear a) what code you are running on the server, b) what code you are running on the client c) if client and server run on same or different systems d) what you expect to happen e) what happens instead. Make sure to strip down anything not needed from the code so you can provide us with the full code you are using and which can be used by others to reproduce your problem. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Steffen Ullrich Nov 05 '22 at 16:44
  • @SteffenUllrich I have updated my post with further information. Please let me know if it's acceptable. The other server code base is a separate project that was written differently than my implementation. It was just to narrow down if anything was being transmitted by the client and to demonstrate my expectations. – user460178 Nov 05 '22 at 16:46

2 Answers2

0
 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 ...

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
0

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thank you very much! I spotted the conn issue as well [from this post](https://stackoverflow.com/questions/28877472/python-socket-winerror-10057). – user460178 Nov 05 '22 at 16:55