|This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs.|
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'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().
- 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.
- 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.
- 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.
A descriptor referencing the socket, or -1 if an error occurs (errno is set).
- You tried to create a socket with a port that's already been allocated.
- You tried to create a socket with a network address for which no network interface exists.
- The remote peer actively refused connection establishment (usually because no process was listening to the port).
- The remote peer forced the connection to be closed.
- You tried to establish a connection on a socket that already has one.
- The system ran out of memory for an internal data structure.
- A connection was dropped due to excessive retransmissions.
Based on RFC 793