Is there any data missing?
Here is some code quoted from Douglas.E.Comer's < Computer Networks and Internets > 4th edition. This program will send back any data it received.
...
while((len = recv(conn, buff, BUFFERSIZE,0)) >0) // receive data
send(conn, buff, len, 0); // send it back
...
I am wondering, what if some data arrived when the 开发者_JAVA百科code is executing in send(..) function, will it miss that data? Because the recv() function is not being executed. If no data is missed, where is the data kept? And by whom?
Thanks...
Incoming data is buffered by the system until the next time the recv()
function is called. This data that hasn't been read yet is stored in buffers inside the operating system and not inside your application. You will not "miss" incoming data using a loop like this.
Every socket has a associated memory buffer where the data is being buffered, when you call recv API the data is read from buffer if some data is present in buffer other wise the call wait till some data is available in socket buffer.
精彩评论