Node:Protocols Database, Next:Ports, Previous:Host Addresses, Up:Internet Namespace
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 getprotobyname
function.
Here are detailed descriptions of the utilities for accessing the
protocols database. These are declared in netdb.h
.
struct protoent | Data Type |
This data type is used to represent entries in the network protocols
database. It has the following members:
|
You can use getprotobyname
and 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.
struct protoent * getprotobyname (const char *name) | Function |
The getprotobyname function returns information about the
network protocol named name. If there is no such protocol, it
returns a null pointer.
|
struct protoent * getprotobynumber (int protocol) | Function |
The getprotobynumber function 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
using setprotoent
, getprotoent
and endprotoent
.
Be careful when using these functions because they are not reentrant.
void setprotoent (int stayopen) | Function |
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 |
struct protoent * getprotoent (void) | Function |
This function returns the next entry in the protocols database. It returns a null pointer if there are no more entries. |
void endprotoent (void) | Function |
This function closes the protocols database. |