The communications protocol used with a socket controls low-level details of how data are exchanged. For example, the protocol implements things like checksums to detect errors in transmissions, and routing instructions for messages. Normal user programs have little reason to mess with these details directly.
The default communications protocol for the Internet namespace depends on the communication style. For stream communication, the default is TCP (“transmission control protocol”). For datagram communication, the default is UDP (“user datagram protocol”). For reliable datagram communication, the default is RDP (“reliable datagram protocol”). You should nearly always use the default.
Internet protocols are generally specified by a name instead of a
number. The network protocols that a host knows about are stored in a
database. This is usually either derived from the file
/etc/protocols, or it may be an equivalent provided by a name
server. You look up the protocol number associated with a named
protocol in the database using the
Here are detailed descriptions of the utilities for accessing the protocols database. These are declared in netdb.h.
This data type is used to represent entries in the network protocols database. It has the following members:
- This is the official name of the protocol.
- These are alternate names for the protocol, specified as an array of strings. The last element of the array is a null pointer.
- This is the protocol number (in host byte order); use this member as the protocol argument to
You can use
getprotobynumber to search
the protocols database for a specific protocol. The information is
returned in a statically-allocated structure; you must copy the
information if you need to save it across calls.
getprotobynamefunction returns information about the network protocol named name. If there is no such protocol, it returns a null pointer.
getprotobynumberfunction returns information about the network protocol with number protocol. If there is no such protocol, it returns a null pointer.
You can also scan the whole protocols database one protocol at a time by
Be careful when using these functions because they are not reentrant.
This function opens the protocols database to begin scanning it.
If the stayopen argument is nonzero, this sets a flag so that subsequent calls to
getprotobynumberwill not close the database (as they usually would). This makes for more efficiency if you call those functions several times, by avoiding reopening the database for each call.
This function returns the next entry in the protocols database. It returns a null pointer if there are no more entries.