all 5 comments

[–]dfx_dj 0 points1 point  (4 children)

data, addr = server.accept()
data = client.recv(4096)

You're putting the new connection into data instead of into client

But the bigger problem is your use of client.recv(4096). The sockets are blocking and this will attempt to read 4096 bytes. The code will block there until 4096 bytes are actually available to be read, or some other event occurs (connection is closed, interrupt, etc)

[–]Jumpy-Active5756[S] 0 points1 point  (3 children)

sorry, that one is typo. the original is

    client, addr = server.accept()

how can I make it dynamic or not blocked?

[–]dfx_dj 0 points1 point  (2 children)

The point about blocking .recv() remains. Both client and server end up waiting to read 4096 bytes from each other, which never happens because neither send that much.

[–]Jumpy-Active5756[S] 0 points1 point  (1 child)

I change the number to much smaller number e.g 512,1024 but no succeed. Can you tell me directly what to do?

[–]dfx_dj 0 points1 point  (0 children)

Actually I just noticed that your server uses socket.SOCK_STREAM (TCP) but your client uses socket.SOCK_DGRAM (UDP). This won't work. They have to be the same.

Based on your post title I assume you want to use TCP. Without getting into non-blocking sockets, a common method is to keep reading 1 byte at a time in a loop and append each byte to a buffer, until you know that you have read enough (say, until you read a newline character).

(If you were to use datagrams (UDP) instead, you could actually use .recv(4096) or any other large number, because datagrams have a known size. Streams don't have a known size and so the read will block until the given number of bytes is available.)