I have been going through the GNU, libc manual on using the sockets. As per to the documentation here
Exact wording from the documentation is,
zero is usually right for protocol.
I have seen several examples of code where engineers have put 0 for the protocol when implementing a socket regardless of the protocol in use!
int socket (int namespace, int style, int protocol)
May I know, What does the author mean by above statement ? I could sense that there are situations in which 0 might not be the correct answer as per to the author's statement, If so, would it be possible for someone to elaborate on such scenarios ? Most importantly where do we get the value for protocol ? Is there any standard to refer to for protocol numbers ?
The valid values for protocol
is dependent on the namespace
used and the kernel the program is running on.
I assume, since the question is tagged gnu
, that you are running under linux. You may then find a list of valid namespaces, and links to documentation of those namespaces with man 'socket(2)'
. For instance, you may find documentation for the internet namespaces AM_INET
and AM_INET6
under man 'ip(7)'
and man 'ipv6(7)'
Reading the ip(7)
manpage you may then see that AM_INET
supports protocols IPPROTO_TCP
and IPPROTO_SCTP
for stream sockets, and IPPROTO_UDP
and IPPROTO_UDPLITE
for datagram sockets.
If you want to dig further, the different protocols also have their own manpages