Internet Transmission Control Protocol
#include <sys/socket.h> #include <netinet/in.h> int socket( AF_INET, SOCK_STREAM, 0 );
The TCP protocol provides reliable, flow-controlled, two-way transmission of data. It is a byte-stream protocol used to support the SOCK_STREAM abstraction.
TCP uses the standard Internet address format and also provides a per-host collection of ``port addresses.'' Thus, each address is composed of an Internet address specifying the host and network, with a specific TCP port on the host identifying the peer entity.
Sockets using the TCP protocol are either ``active'' or ``passive.'' Active sockets initiate connections to passive sockets. By default, TCP sockets are created active.
To create a passive socket, you must bind the socket with the bind() system call and then use the listen() system call. Only passive sockets may use the accept() call to accept incoming connections; only active sockets may use the connect() call to initiate connections.
Passive sockets may ``underspecify'' their location to match incoming connection requests from multiple networks. With this technique, termed wildcard addressing, a single server can provide service to clients on multiple networks. If you wish to create a socket that listens on all networks, the Internet address INADDR_ANY must be bound. You can still specify the TCP port at this time; if the port isn't specified, the system will assign one.
Once a connection has been established, the socket's address is fixed by the peer entity's location. The address assigned to the socket is the address associated with the network interface through which packets are being transmitted and received. Normally this address corresponds to the peer entity's network.
TCP supports several socket options (defined in <netinet/tcp.h>) that are set with setsockopt() and retrieved with getsockopt(). The option level for these call is the protocol number for TCP, available from getprotobyname().
For a few clients (such as windowing systems that send a stream of mouse events that receive no replies), this packetization may cause significant delays. Therefore, TCP provides a boolean option, TCP_NODELAY, to defeat this algorithm.
Options at the IP transport level may be used with TCP (see the IP protocol. Incoming connection requests that are source-routed are noted, and the reverse source route is used in responding.
A descriptor referencing the socket, or -1 if an error occurred (errno is set).
If an error occurred, errno could contain one of the following:
IP protocol, getsockopt(), socket()
RFC 793