Question

for non-blocking datagram socket, like UDP, when I call write()/send() on the socket, each call of write()/send() or read()/recv() deal with exactly 1 packet.

I'm wondering if raw socket, like the below, is a datagram socket or not?

int on = 1;
rawfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
setsockopt(IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));
Was it helpful?

Solution

That depends on the kind of IP header you will include in your packets (TCP or UDP). Actually it's more easier to include the UDP header since the kernel will manage some TCP mechanism.

So you have to add the UDP header in your packets, then it will be a datagram socket.

OTHER TIPS

When you send data out, TCP/IP stack will add TCP/UDP header, IP header and then Ethernet header, and network work card transmit the whole packet out. For raw socket, you prepare all the headers(TCP/UDP, IP and MAC), and network work card transmit the whole packet out. So whether it is datagram depends on the header you add is TCP or UDP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top