Socket issues in Python

bynx

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't receive a specific message), but for some reason, every other time I run it, it goes through the other statement in my code and is really inconsistent - sometimes it will work as it should and then it will randomly break for a couple uses.

Here is the code on my client side:

def post_checker(client_socket):
    response = client_socket.recv(1024)

    #check if response is "NP" for a new post from another user
    if response == "NP":
        new_response = client_socket.recv(1024)
        print new_response
    else: #print original message being sent
        print response

where post_checker is called in the main function as simply "post_checker(client_socket)" Basically, sometimes I get "NPray" printed to my console (when the client only expects to receive the username "ray") and other times it will print correctly.

Here is the server code correlated to this

for sublist in user_list:
    client_socket.send("NP")
    client_socket.send(sublist[1] + " ")

where user_list is a nested list and sublist[1] is the username I wish to print out on the client side.

Whats going on here?

Brian Cain

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:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related