Saturday, August 6, 2011

Choosing protocol argument of the socket


You might think that with the protocol family specified, and the socket type specified for a new socket, there would be little need for anything else. Although normally there is only one protocol used for a given protocol family and socket type, there are some situations where this isn't true. The protocol parameter of the socket(2) or socketpair(2) function allows you to be more
specific when this need arises. The good news is that it is rare that you need to choose a value for this parameter. Normally, you
simply specify zero for the protocol. This allows the Linux kernel to choose the correct protocol for the other parameters that you have specified.

NOTE
The protocol argument of the socket(2) or socketpair(2) function is normally supplied as the value zero. This tells the kernel to choose the correct protocol according to the domain and socket type parameter values chosen. See protocols(5) or the include file

<netinet/in.h> for macro entries such as IPPROTO_TCP.

Some programmers prefer, however, to explicitly describe the protocol argument value. This might be important for certain applications in which a specific protocol is required, and no substitution is permitted. This allows you to choose the final protocol used rather than rely on today's "kernel of the month." The downside of this is that when networks and protocols evolve, someone might have to go back and revisit your source code to make it work.

No comments:

Post a Comment