a_confused_student
a_confused_student

Reputation: 405

Protocol number for socket creation for UDP protocol

I am in the process of making a socket for a client in C. I would like to use the UDP protocol, but I can't seem to find it in the man pages.

int socket(int domain, int type, int protocol);

Upvotes: 1

Views: 445

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69367

The man 2 socket manual page only describes the general usage of the socket() system call. It does not go into much detail about each possible protocol. For that, there are several different manual pages under section 7.

For the IP protocol, you can check man 7 ip, which states:

An IP socket is created using socket(2):

    socket(AF_INET, socket_type, protocol);

Valid socket types include SOCK_STREAM to open a stream socket,
SOCK_DGRAM to open a datagram socket, and SOCK_RAW to open a
raw(7) socket to access the IP protocol directly.

protocol is the IP protocol in the IP header to be received or
sent.  Valid values for protocol include:

   - 0 and IPPROTO_TCP for tcp(7) stream sockets;
   - 0 and IPPROTO_UDP for udp(7) datagram sockets;
   - IPPROTO_SCTP for sctp(7) stream sockets; and
   - IPPROTO_UDPLITE for udplite(7) datagram sockets.

So for an UDP socket you want:

int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

Or equivalently:

int fd = socket(AF_INET, SOCK_DGRAM, 0);

Upvotes: 3

Related Questions