0

Hello I have written some client server code and write now I noticed I had a bug in how I am handling receiving a command

These are my commands

#Server Commands
CMD_MSG, CMD_MULTI, CMD_IP, CMD_AUDIO, CMD_AUDIO_MULTI, CMD_FILE = range(6)

I send a command like this

self.client(chr(CMD_AUDIO), data)

and receive like this

msg = conn.recv(2024)
if msg:                
    cmd, msg = ord(msg[0]),msg[1:]
    if cmd == CMD_MSG:
        #do something

The first command seems to work but if I call any other it seems to loop through them all. Its really bizarre

I can post more code if needed.

But any ideas on how to handle the commands being sent to my server would be great

*cheers

1 Answer 1

1

Assuming you're using a stream (TCP) socket, the first rule of stream sockets is that you will not receive data in the same groups it is sent. If you send three messages of 10 bytes each, you may receive at the other end one block of 30 bytes, 30 blocks of one byte each, or anything in between.

You must structure your protocol so that the receiver knows how long each message within the stream is (either by adding a length field, or by having fixed length message formats), and you must save the unused portion of any recv() that crosses a message boundary to use in the next message.

The alternative to stream/TCP sockets is datagram/UDP sockets. These preserve message boundaries, but do not guarantee delivery or ordering of the messages. Depending on what you're doing, this may be acceptable, but probably not.

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

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.