Reputation: 11
I've spent already some weeks investigating how Linux tcp/ip stack works. And the most difficult part for me is network sockets. I have a lot of questions but I will do my best to be as specific as possible. Let's talk about udp sockets for simplicity.
recvfrom()
is just a case of read()
syscall for copying data from the socket buffer inside kernel to the user space. I looked through the implementation and all user space operations end at the point sock->ops->some-protocol-dependent-operation
(i.e. sock->ops->recvmsg
). Correct me if I am wrong.recvfrom
becomes unblocked. The next question: Is recvfrom
just make process sleep (what mechanism is used? sigwait?). Or Is there a long polling somewhere inside of this stacktrace?Guys, really sorry for so broad topic but everything is interconnected and I cannot split it to different parts.
Upvotes: 1
Views: 281
Reputation: 1
I think you have a correct approach for the first question
Then, UDP is not connected -> you will only need one recvfrom (). However TCP is connected and so you will "keep" your connexion and so you will also maybe need several recv() (and so to make a loop) [I don't really know if it is what you want to know...]
recv() and recvfrom() are blocking functions (they wait a new message), and so to unblock them you will need fcntl () [e.g: fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK)
]. And then, you can verify that you have new message with loops. But I think this is easier using select () (you have'nt to use fcntl() and it allow to use a socket only when it is activated by an activity [reception here]).
I hope this will answer your questions (and I hope I say nothing bad)
Upvotes: 0