I am creating a little application in C using UDP sockets and I am using the following recvfrom function:
int recvfrom(int s, void *buf, int len, unsigned int flags struct sockaddr *from, socklen_t *fromlen);
I am actually wondering from where the data are retrieved by this function because in my application, I receive different packets and once I’m sure that I received all the packets, I use a loop to retrieve several packets.
It seems to work (maybe I am lucky 😉 but I don’t really understand where my packets are stored before to be retrieved by the multiple calls to recvfrom in my loop.
Thank you in advance for your help and have a good day !
Alex
They are held in the kernel, in some internal buffer. Should that buffer fill (i.e. if your application stops calling
recvfrom) the kernel will start dropping datagrams.It’s important to realize that both
sendtoandrecvfromare just fancymemcpy-like calls – none of them actually “send” or “receive” anything. Sendto copies data to the kernel, and then the kernel tries to actually put it into packets and so on. Similarly, by the time you callrecvfrom, the data has already been received andrecvfromonly copies it to your userspace buffer.