Only receiving one byte from socket
With TCP/IP connections your message can be fragmented. It might send one letter at a time, or it might send the whole lot at once - you can never be sure.
Your programs needs to be able to handle this fragmentation. Either use a fixed length packet (so you always read X bytes) or send the length of the data at the start of each packet. If you are only sending ASCII letters, you can also use a specific character (eg \n
) to mark the end of transmission. In this case you would read until the message contains a \n
.
recv(200)
isn't guaranteed to receive 200 bytes - 200 is just the maximum.
This is an example of how your server could look:
rec = ""
while True:
rec += connection.recv(1024)
rec_end = rec.find('\n')
if rec_end != -1:
data = rec[:rec_end]
# Do whatever you want with data here
rec = rec[rec_end+1:]