Reputation: 57
I am trying to find out why the loop is not breaking up, but instead it just stuck in prompt. Am I missing something really easy and stupid or is it because my current setup?
Doing just simple socket communication between two points.
Server sending data I need and host received them, but script from some reason not finished.
buffer = ''
while True:
data = s.recv(1024)
recv_dec = data.decode('utf-8')
if recv_dec:
buffer += recv_dec
print(buffer)
else:
break
I thought that logic should be right, or ?
By stuck mean, asking prompt or how this step called:
Sorry btw, I am really begginer.
Upvotes: 0
Views: 359
Reputation: 4439
>>> help(socket.socket.recv)
recv(...)
recv(buffersize[, flags]) -> data
Receive up to buffersize bytes from the socket. For the optional flags
argument, see the Unix manual. When no data is available, block until
at least one byte is available or until the remote end is closed. When
the remote end is closed and all data is read, return the empty string.
By default recv
will wait forever for more data to come or until the socket is closed on the other end. So it's not that your loop is looping forever it's that it's stuck waiting for data to be sent from the server. To make recv
return immediately if no data is available use the MSG_DONTWAIT
flag (or equivalent, maybe depending on your operating system). You might now run into the problem that recv
returns prematurely with no data because it's reading faster than the server is writing but that's the gist of it.
Upvotes: 1