Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, for a socket, does recvmsg() have a queue?

Tags:

c

sockets

recv

If a C socket's recvmsg() has a queue, how can I find out how many items are backlogged in the queue?

My problem is that the speed of the code after I receive something from recvmsg() is sometimes slower than the rate of data sent to recvmsg(), which would logically result in a queue? What happens if the queue becomes too big?

For example if this is my recv() code:

while (recvmsg(SocketA,...) > 0)
{
    ...
    ...something that takes 1.5 seconds to execute/complete...
    ...
}

and if the following function somewhere else gets called every 1 second:

// gets called every 1 second
int send_to_sock()
{
    ...
    send(SocketA, ...);
    ...
}

I checked the reference pages for recvmsg() but none of them mention anything about a queue, but it seems like there is a queue because I am observing delays that incrementally add up. But the code stops working if the queue gets too long, so I want to know if there is a way to check length of queue.

like image 725
Fuad Avatar asked Dec 28 '25 05:12

Fuad


2 Answers

It's not a queue, it's a buffer associated with most of I/O devices, not only sockets. For example, when you read from stdin with something like scanf, data goes to your program when you press enter. Where do you think all keystrokes are stored in the meantime?

The specific details of these buffers are implementation defined, but usually what happens with sockets is that new packets are discarded if the buffer is full. You can find more information about querying the state of the buffer in How to find the socket buffer size of linux and How can I tell if a socket buffer is full?.

like image 196
Paul92 Avatar answered Dec 30 '25 20:12

Paul92


Yes. There is a socket receive buffer. Its maximum size can be got and set via getsockopt() and setsockopt() using the SO_RCVBUF option. When it fills, in the case of TCP the sender is told to stop sending; in the case of UDP further incoming data is discarded.

like image 38
user207421 Avatar answered Dec 30 '25 21:12

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!