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().
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.
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).
accept(), bind(), connect(), getprotobyname(), getsockopt(), listen(), setsockopt(), socket()
Based on RFC 793