2

I am writing python code to send and receive data, using TCP connection. But I have a problem which I find very confusing. When I call recv method on the server socket once and then send, it works fine. However, when I call recv method twice on the server socket and then send, the client does not receive anything from the server but just hangs.

Here is the server code:

server = socket.socket()
server.bind(('0.0.0.0', 9999))
server.listen(5)
while True:
    client, addr = server.accept()
    client.recv(1024)
    # works if the line below is commented out
    client.recv(1024)
    client.send(b'ACK!')
    client.close()

Here is the client code:

client = socket.socket()
client.connect(('127.0.0.1', 9999))
client.send(bytes(sys.stdin.read(), encoding='utf-8'))
print(client.recv(4096).decode('utf-8'))

Actually, I plan to use a loop with the recv method in the server code. But the send method does not work if the recv method is called more than once. Why does this happen?

2
  • The server-side client.recv(1024) is probably blocked waiting for data. What did you expect to happen? Commented Apr 6, 2018 at 15:33
  • @JamesKPolk I expected the server to receive all data (so the second call of recv will receive nothing) and then send data. But as you said, the server might be waiting for data in the second call of recv. I think I might need to use settimeout. Thank you for the answer. Commented Apr 7, 2018 at 1:35

3 Answers 3

3

client.recv(1024) does not mean block until you recv 1024 bytes, you're not guaranteed to get 1024 bytes with this call, you're only guaranteed to get no more than 1024 bytes. You could get 0, 9, 25, etc. bytes, just no more than 1024. The real issue is that your sequence isn't quite right. On the server side you're calling recv twice and then send, on the client side you're calling send once then recv. If you're going to recv twice on the server then you need to send twice on the client.

The issue is that you're being blocked because you're not sending ANY data, when it's expecting something.

Sign up to request clarification or add additional context in comments.

Comments

0

I do not really think that is your main problem. You also did not specify the connection type (tcp or udp).

For TCP you call socket.socket(socket.SOCK_STREAM, socket.AF_INET)

For UDP you call socket.socket(socket.SOCK_STREAM, socket.SOCK_DGRAM)

I do not know if this will fix your problem, but I hope so.

Comments

0

None of the answers currently given will solve your problem. Your socket() construction is fine with default parameters, and the number of sends and receives doesn't matter at all. The real answer is this:

When you recv(1024), you're going to get at least one, and at most 1024 bytes. You probably know the latter, but the former might be a surprise. It is a consequence of how network works. recv must block (unless the socket is in nonblocking mode) until it gets something, and that something will be at least one byte. So if there's nothing to read, recv will hang. It can't detect the difference between that situation and the network simply being slow.

If you have further questions, feel free to ask.

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.