1TCP(4P) TCP(4P)
2
3
4
6 tcp - Internet Transmission Control Protocol
7
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11
12 s = socket(AF_INET, SOCK_STREAM, 0);
13
15 The TCP protocol provides reliable, flow-controlled, two-way transmis‐
16 sion of data. It is a byte-stream protocol used to support the
17 SOCK_STREAM abstraction. TCP uses the standard Internet address format
18 and, in addition, provides a per-host collection of “port addresses”.
19 Thus, each address is composed of an Internet address specifying the
20 host and network, with a specific TCP port on the host identifying the
21 peer entity.
22
23 Sockets utilizing the tcp protocol are either “active” or “passive”.
24 Active sockets initiate connections to passive sockets. By default TCP
25 sockets are created active; to create a passive socket the listen(2)
26 system call must be used after binding the socket with the bind(2) sys‐
27 tem call. Only passive sockets may use the accept(2) call to accept
28 incoming connections. Only active sockets may use the connect(2) call
29 to initiate connections.
30
31 Passive sockets may “underspecify” their location to match incoming
32 connection requests from multiple networks. This technique, termed
33 “wildcard addressing”, allows a single server to provide service to
34 clients on multiple networks. To create a socket which listens on all
35 networks, the Internet address INADDR_ANY must be bound. The TCP port
36 may still be specified at this time; if the port is not specified the
37 system will assign one. Once a connection has been established the
38 socket's address is fixed by the peer entity's location. The address
39 assigned the socket is the address associated with the network inter‐
40 face through which packets are being transmitted and received. Nor‐
41 mally this address corresponds to the peer entity's network.
42
43 TCP supports one socket option which is set with setsockopt(2) and
44 tested with getsockopt(2). Under most circumstances, TCP sends data
45 when it is presented; when outstanding data has not yet been acknowl‐
46 edged, it gathers small amounts of output to be sent in a single packet
47 once an acknowledgement is received. For a small number of clients,
48 such as window systems that send a stream of mouse events which receive
49 no replies, this packetization may cause significant delays. There‐
50 fore, TCP provides a boolean option, TCP_NODELAY (from <netinet/tcp.h>,
51 to defeat this algorithm. The option level for the setsockopt call is
52 the protocol number for TCP, available from getprotobyname(3N).
53
54 Options at the IP transport level may be used with TCP; see ip(4P).
55 Incoming connection requests that are source-routed are noted, and the
56 reverse source route is used in responding.
57
59 A socket operation may fail with one of the following errors returned:
60
61 [EISCONN] when trying to establish a connection on a socket
62 which already has one;
63
64 [ENOBUFS] when the system runs out of memory for an internal
65 data structure;
66
67 [ETIMEDOUT] when a connection was dropped due to excessive
68 retransmissions;
69
70 [ECONNRESET] when the remote peer forces the connection to be
71 closed;
72
73 [ECONNREFUSED] when the remote peer actively refuses connection
74 establishment (usually because no process is lis‐
75 tening to the port);
76
77 [EADDRINUSE] when an attempt is made to create a socket with a
78 port which has already been allocated;
79
80 [EADDRNOTAVAIL] when an attempt is made to create a socket with a
81 network address for which no network interface
82 exists.
83
85 getsockopt(2), socket(2), intro(4N), inet(4F), ip(4P)
86
87
88
894.2 Berkeley Distribution May 16, 1986 TCP(4P)