Caution: This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs.

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's 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 assigns 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 you can set with setsockopt() and retrieve with getsockopt(). The option level for these calls 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
The Maximum Segment Size (MSS) for a TCP connection. The value returned is the maximum amount of data that TCP sends to the other end. If this value is fetched before the socket is connected, the value returned is the default value that's used if an MSS option isn't received from the other end.
TCP_KEEPALIVE
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.

You can use options at the IP transport level 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 occurs (errno is set).

Errors:

EADDRINUSE
You tried to create a socket with a port that's already been allocated.
EADDRNOTAVAIL
You 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
You 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

accept(), bind(), connect(), getprotobyname(), getsockopt(), listen(), setsockopt(), socket()

Based on RFC 793