Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the C functions recvfrom and sendto mutually exclusive?

I have one thread calling recvfrom with a timeout of 100 ms in a loop. Another thread calls sendto periodically.

Does the sendto function wait until the recvfrom is released by the timeout (or a successful read) or does it send the data during that period of time.

like image 646
Pepedou Avatar asked Jun 16 '15 17:06

Pepedou


People also ask

What does Recvfrom return in C?

Return value If no error occurs, recvfrom returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

What is the difference between RECV and Recvfrom?

From the point of view of TCP/IP, the RECV, RECVFROM, and RECVMSG calls are identical. From the point of view of the application, RECVFROM differs from RECV in that RECVFROM additionally provides the source address of the message.

What is the use of socket Recvfrom () method?

The recvfrom() function receives data on a socket named by descriptor socket and stores it in a buffer. The recvfrom() function applies to any datagram socket, whether connected or unconnected. The socket descriptor. The pointer to the buffer that receives the data.

Is Recvfrom blocking?

By default, Recvfrom() is blocking: when a process issues a Recvfrom() that cannot be completed immediately (because there is no packet), the process is put to sleep waiting for a packet to arrive at the socket. Therefore, a call to Recvfrom() will return immediately only if a packet is available on the socket.


2 Answers

Are the C functions recvfrom and sendto mutually exclusive?

No. They can both be executed by different threads at the same time.

sendto() doesn't wait for recvfrom() to read the data. It would place the data into the socket's buffer and return. Multiple sendto() may block for the previous sendto() to complete. If any error occurred (buffer full, message too big etc) while sending then you can check inspect errno to check the cause of failure. Basically, you don't need to do any synchronization between sendto() and recvfrom() calls from the two threads; they are atomic operations.

like image 194
P.P Avatar answered Oct 10 '22 12:10

P.P


No, it does not wait, (at least, it does not wait any longer than necessary to gain thread-safe access to the comms stack).

like image 1
Martin James Avatar answered Oct 10 '22 12:10

Martin James