Skip to content Skip to sidebar Skip to footer

Socket Issues In Python

I'm building a simple server-client app using sockets. Right now, I am trying to get my client to print to console only when it received a specific message (actually, when it doesn

Solution 1:

The nature of your problem is that TCP is a streaming protocol. The bufsize in recv(bufsize) is a maximum size. The recv function will always return data when available, even if not all of the bytes have been received.
See the documentation for details.

This causes problems when you've only sent half the bytes, but you've already started processing the data. I suggest you take a look at the "recvall" concept from this site or you can also consider using UDP sockets (which would solve this problem but may create a host of others as UDP is not a guaranteed protocol).

You may also want to let the python packages handle some of the underlying framework for you. Consider using a SocketServer as documented here:


Post a Comment for "Socket Issues In Python"