Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C socket: does send wait for recv to end?

I use blocking C sockets on Windows. I use them to send updates of a data from the server to the client and vice versa. I send updates at a high frequency (every 100ms). Does the send() function will wait for the recipient recv() to receive the data before ending ?

I assume not if I understand well the man page:

"Successful completion of send() does not guarantee delivery of the message."

So what will happen if one is running 10 send() occurences while the other has only complete 1 recv() ?

Do I need to use so some sort of acknowledgement system ?

like image 413
Giann Avatar asked Aug 26 '10 19:08

Giann


People also ask

Does socket recv wait for data?

The recv() function receives a message from a socket. The recv() call can be used on a connection mode socket or a bound, connectionless socket. If no messages are available at the socket, the recv() call waits for a message to arrive unless the socket is nonblocking.

Is recv blocking in C?

recv will block until the entire buffer is filled, or the socket is closed. If you want to read length bytes and return, then you must only pass to recv a buffer of size length . This can avoid recv from blocking.

Is recv () a blocking call?

recv(IPC, Buffer, int n) is a blocking call, that is, if data is available it writes it to the buffer and immediately returns true, and if no data is available it waits for at least n seconds to receive any data.

Can a socket send and receive at the same time?

You can send and receive on the same socket at the same time (via multiple threads). But the send and receive may not actually occur simultaneously, since one operation may block the other from starting until it's done. That is how it's in Winsock.


1 Answers

Lets assume you are using TCP. When you call send, the data that you are sending is immediately placed on the outgoing queue and send then completes successfully. If however, send is unable to place the data on the outgoing queue, send will return with an error.

Since Tcp is a guaranteed delivery protocol, the data on the outgoing queue can only be removed once acknowledgement has been received by the remote end. This is because the data may need to be resent if no ack has been received in time.

If the remote end is sluggish, the outgoing queue will fill up with data and send will then block until there is space to place the new data on the outgoing queue.

The connection can however fail is such a way that there is no way any further data can be sent. Although once a TCP connection has been closed, any further sends will result in an error, the user has no way of knowing how much data did actually make it to the other side. (I know of no way of retrieving TCP bookkeeping from a socket to the user application). Therefore, if confirmation of receipt of data is required, you should probably implement this on application level.

For UDP, I think it goes without saying that some way of reporting what has or has not been received is a must.

like image 93
doron Avatar answered Nov 16 '22 22:11

doron