Receive complete message (network programming)
-
I'm designing a protocol for my chatserver that I'm building. But I've run into a problem about receiving messages. How can I determine that the message was completely received? Let's say this is my message (an example only):
MSG\r\n
Content-Type: text/plain\r\n
Extras: font=Arial; style=bold;\r\n\r\n
This is where the body goes, after a double '\r\n' mark\r\nNow to my problem. Let's say I receive only 16 bytes at the time (in reality you don't do this, I know). This means I'll have to call recv() more than one time in this example. So, how can I determine when I've received the complete message? Should I put some ending mark like a tripple \r\n or something? Should I send the complete size of the message after MSG? Do you have any good algorithm to share? Another thing also when it's in mind: What if I receive the end of one message and the beginning of another at the same recv() call? Like this:
fter a double '\r\n' mark\r\n
MSG\r\n
Content-Type: bla blaBut what I understand from my book, in order to accokmplish that I have send the end and the beginning in the same buffer with send()? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318
-
I'm designing a protocol for my chatserver that I'm building. But I've run into a problem about receiving messages. How can I determine that the message was completely received? Let's say this is my message (an example only):
MSG\r\n
Content-Type: text/plain\r\n
Extras: font=Arial; style=bold;\r\n\r\n
This is where the body goes, after a double '\r\n' mark\r\nNow to my problem. Let's say I receive only 16 bytes at the time (in reality you don't do this, I know). This means I'll have to call recv() more than one time in this example. So, how can I determine when I've received the complete message? Should I put some ending mark like a tripple \r\n or something? Should I send the complete size of the message after MSG? Do you have any good algorithm to share? Another thing also when it's in mind: What if I receive the end of one message and the beginning of another at the same recv() call? Like this:
fter a double '\r\n' mark\r\n
MSG\r\n
Content-Type: bla blaBut what I understand from my book, in order to accokmplish that I have send the end and the beginning in the same buffer with send()? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318
I think I have to send the WHOLE message and then also receive the WHOLE message. ElseI have to send the size as a field or something, I don't know... Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318
-
I'm designing a protocol for my chatserver that I'm building. But I've run into a problem about receiving messages. How can I determine that the message was completely received? Let's say this is my message (an example only):
MSG\r\n
Content-Type: text/plain\r\n
Extras: font=Arial; style=bold;\r\n\r\n
This is where the body goes, after a double '\r\n' mark\r\nNow to my problem. Let's say I receive only 16 bytes at the time (in reality you don't do this, I know). This means I'll have to call recv() more than one time in this example. So, how can I determine when I've received the complete message? Should I put some ending mark like a tripple \r\n or something? Should I send the complete size of the message after MSG? Do you have any good algorithm to share? Another thing also when it's in mind: What if I receive the end of one message and the beginning of another at the same recv() call? Like this:
fter a double '\r\n' mark\r\n
MSG\r\n
Content-Type: bla blaBut what I understand from my book, in order to accokmplish that I have send the end and the beginning in the same buffer with send()? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318
Either send the length of message before message text (like HTTP does) or use a special mark at the end of the message (like SMTP does). Both approaches are proven to work well enough.
In your second problem, you can't assume that blocks you
send()
match the blocks youreceive()
if you use TCP. The network is free to split or combine data as it sees fit.