TCP

Internet Transmission Control Protocol

Synopsis:

#include <sys/socket.h>
#include <netinet/in.h>

int socket( AF_INET, 
            SOCK_STREAM, 
            0 );

Description:

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().

TCP_NODELAY
Under most circumstances, TCP sends data when it's presented. When outstanding data hasn't yet been acknowledged, TCP gathers small amounts of output to be sent in a single packet once an acknowledgment is received.

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.

TCP_MAXSEG
This integer option allows the user to fetch or set the maximum segment size (MSS) for a TCP connection. The value returned is the maximum amount of data that TCP will send to the other end; If this value is fetched before the socket is connected, the value returned is the default value that will be used if an MSS option isn't received from the other end.
TCP_KEEPALIVE
This integer option specifies the idle time in seconds for the connection before TCP starts sending keepalive probes. The default value is 2 hours. This option is effective only when the SO_KEEPALIVE socket option is enabled.

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.

Returns:

A descriptor referencing the socket, or -1 if an error occurred (errno is set).

Errors:

If an error occurred, errno could contain one of the following:

EADDRINUSE
Tried to create a socket with a port that has already been allocated.
EADDRNOTAVAIL
Tried to create a socket with a network address for which no network interface exists.
ECONNREFUSED
The remote peer actively refused connection establishment (usually because no process was listening to the port).
ECONNRESET
The remote peer forced the connection to be closed.
EISCONN
Tried to establish a connection on a socket that already has one.
ENOBUFS
The system ran out of memory for an internal data structure.
ETIMEDOUT
A connection was dropped due to excessive retransmissions.

See also:

IP protocol, getsockopt(), socket()

RFC 793