1.\" Copyright (c) 1983, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" %sccs.include.redist.man% 5.\" 6.\" @(#)tcp.4 6.5 (Berkeley) 03/28/91 7.\" 8.Dd 9.Dt TCP 4 10.Os BSD 4.2 11.Sh NAME 12.Nm tcp 13.Nd Internet Transmission Control Protocol 14.Sh SYNOPSIS 15.Fd #include <sys/socket.h> 16.Fd #include <netinet/in.h> 17.Ft int 18.Fn socket AF_INET SOCK_STREAM 0 19.Sh DESCRIPTION 20The 21.Tn TCP 22protocol provides reliable, flow-controlled, two-way 23transmission of data. It is a byte-stream protocol used to 24support the 25.Dv SOCK_STREAM 26abstraction. TCP uses the standard 27Internet address format and, in addition, provides a per-host 28collection of 29.Dq port addresses . 30Thus, each address is composed 31of an Internet address specifying the host and network, with 32a specific 33.Tn TCP 34port on the host identifying the peer entity. 35.Pp 36Sockets utilizing the tcp protocol are either 37.Dq active 38or 39.Dq passive . 40Active sockets initiate connections to passive 41sockets. By default 42.Tn TCP 43sockets are created active; to create a 44passive socket the 45.Xr listen 2 46system call must be used 47after binding the socket with the 48.Xr bind 2 49system call. Only 50passive sockets may use the 51.Xr accept 2 52call to accept incoming connections. Only active sockets may 53use the 54.Xr connect 2 55call to initiate connections. 56.Pp 57Passive sockets may 58.Dq underspecify 59their location to match 60incoming connection requests from multiple networks. This 61technique, termed 62.Dq wildcard addressing , 63allows a single 64server to provide service to clients on multiple networks. 65To create a socket which listens on all networks, the Internet 66address 67.Dv INADDR_ANY 68must be bound. The 69.Tn TCP 70port may still be specified 71at this time; if the port is not specified the system will assign one. 72Once a connection has been established the socket's address is 73fixed by the peer entity's location. The address assigned the 74socket is the address associated with the network interface 75through which packets are being transmitted and received. Normally 76this address corresponds to the peer entity's network. 77.Pp 78.Tn TCP 79supports one socket option which is set with 80.Xr setsockopt 2 81and tested with 82.Xr getsockopt 2 . 83Under most circumstances, 84.Tn TCP 85sends data when it is presented; 86when outstanding data has not yet been acknowledged, it gathers 87small amounts of output to be sent in a single packet once 88an acknowledgement is received. 89For a small number of clients, such as window systems 90that send a stream of mouse events which receive no replies, 91this packetization may cause significant delays. 92Therefore, 93.Tn TCP 94provides a boolean option, 95.Dv TCP_NODELAY 96(from 97.Aq Pa netinet/tcp.h , 98to defeat this algorithm. 99The option level for the 100.Xr setsockopt 101call is the protocol number for 102.Tn TCP , 103available from 104.Xr getprotobyname 3 . 105.Pp 106Options at the 107.Tn IP 108transport level may be used with 109.Tn TCP ; 110see 111.Xr ip 4 . 112Incoming connection requests that are source-routed are noted, 113and the reverse source route is used in responding. 114.Sh DIAGNOSTICS 115A socket operation may fail with one of the following errors returned: 116.Bl -tag -width [EADDRNOTAVAIL] 117.It Bq Er EISCONN 118when trying to establish a connection on a socket which 119already has one; 120.It Bq Er ENOBUFS 121when the system runs out of memory for 122an internal data structure; 123.It Bq Er ETIMEDOUT 124when a connection was dropped 125due to excessive retransmissions; 126.It Bq Er ECONNRESET 127when the remote peer 128forces the connection to be closed; 129.It Bq Er ECONNREFUSED 130when the remote 131peer actively refuses connection establishment (usually because 132no process is listening to the port); 133.It Bq Er EADDRINUSE 134when an attempt 135is made to create a socket with a port which has already been 136allocated; 137.It Bq Er EADDRNOTAVAIL 138when an attempt is made to create a 139socket with a network address for which no network interface 140exists. 141.El 142.Sh SEE ALSO 143.Xr getsockopt 2 , 144.Xr socket 2 , 145.Xr intro 4 , 146.Xr inet 4 , 147.Xr ip 4 148.Sh HISTORY 149The 150.Nm 151protocol stack appeared in 152.Bx 4.2 . 153