i have a client which sends data to a server with 2 consecutive send calls:
send(_sockfd,msg,150,0);
send(_sockfd,msg,150,0);
and the server is receiving when the first send call was sent (let’s say i’m using select):
recv(_sockfd,buf,700,0);
note that the buffer i’m receiving is much bigger.
my question is: is there any chance that buf will contain both msgs? of do i need 2 recv() calls to get both msgs?
thank you!
TCPis a stream oriented protocol. Not message / record / chunk oriented. That is, all that is guaranteed is that if you send a stream, the bytes will get to the other side in the order you sent them. There is no provision made by RFC 793 or any other document about the number of segments / packets involved.This is in stark contrast with
UDP. As @R.. correctly said, inUDPan entire message is sent in one operation (notice the change in terminology:message). Try to send a giant message (several times larger than the MTU) with TCP ? It’s okay, it will split it for you.When running on local networks or on localhost you will certainly notice that (generally)
one send == one recv. Don’t assume that. There are factors that change it dramatically. Among theseOf course, not having a correspondence between an a
sendand arecvis a nuisance and you can’t rely onUDP. That is one of the reasons forSCTP.SCTPis a really really interesting protocol and it is message-oriented.Back to
TCP, this is a common nuisance. An equally common solution is this:It is really important to notice how there are really no messages on the wire, only bytes. Once you understand it you will have made a giant leap towards writing network applications.