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.
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.
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.
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.
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.
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.
No, it does not wait, (at least, it does not wait any longer than necessary to gain thread-safe access to the comms stack).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With