Search code examples
socketsnetwork-programmingraw-sockets

Is raw socket datagram socket or not?


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

I'm wondering if a 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));

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.