Using recvfrom() with raw sockets : general doubt

孤者浪人 提交于 2019-12-22 14:43:20

问题


I have created a raw socket which takes all IPv4 packets from data link layer (with data link layer header removed). And for reading the packets I use recvfrom.

My doubt is: Suppose due to some scheduling done by OS, my process was asleep for 1 sec. When it woke up,it did recvfrom (with number of bytes to be received say 1000) on this raw socket (with the intention of receiving only one IPv4 packet and say the size of this packet is 380 bytes). And suppose many network applications were also running simultaneously during this time, so all IPv4 packets must have been queued in the receive buffer of this socket. So now recvfrom will return all 1000 bytes (with other IPv4 packets from 381th byte onwards) bcoz it has enough data in its buffer to return. Although my program was meant to understand only one IPv4 packet

So how to prevent this thing? Should I read byte by byte and parse each byte but it is very inefficient.


回答1:


IIRC, recvfrom() will only return one packet at a time, even if there are more in the queue.




回答2:


Raw sockets operate at the packet layer, there is no concept of data streams.

You might be interested in recvmmsg() if you want to read multiple packets in one system call. Recent Linux kernels only, there is no equivalent send side implementation.



来源:https://stackoverflow.com/questions/1795874/using-recvfrom-with-raw-sockets-general-doubt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!