1.\" Copyright (c) 1983, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)tcp.4 8.1 (Berkeley) 6/5/93 33.\" 34.Dd June 5, 1993 35.Dt TCP 4 36.Os BSD 4.2 37.Sh NAME 38.Nm tcp 39.Nd Internet Transmission Control Protocol 40.Sh SYNOPSIS 41.Fd #include <sys/socket.h> 42.Fd #include <netinet/in.h> 43.Ft int 44.Fn socket AF_INET SOCK_STREAM 0 45.Sh DESCRIPTION 46The 47.Tn TCP 48protocol provides reliable, flow-controlled, two-way 49transmission of data. It is a byte-stream protocol used to 50support the 51.Dv SOCK_STREAM 52abstraction. TCP uses the standard 53Internet address format and, in addition, provides a per-host 54collection of 55.Dq port addresses . 56Thus, each address is composed 57of an Internet address specifying the host and network, with 58a specific 59.Tn TCP 60port on the host identifying the peer entity. 61.Pp 62Sockets utilizing the tcp protocol are either 63.Dq active 64or 65.Dq passive . 66Active sockets initiate connections to passive 67sockets. By default 68.Tn TCP 69sockets are created active; to create a 70passive socket the 71.Xr listen 2 72system call must be used 73after binding the socket with the 74.Xr bind 2 75system call. Only 76passive sockets may use the 77.Xr accept 2 78call to accept incoming connections. Only active sockets may 79use the 80.Xr connect 2 81call to initiate connections. 82.Pp 83Passive sockets may 84.Dq underspecify 85their location to match 86incoming connection requests from multiple networks. This 87technique, termed 88.Dq wildcard addressing , 89allows a single 90server to provide service to clients on multiple networks. 91To create a socket which listens on all networks, the Internet 92address 93.Dv INADDR_ANY 94must be bound. The 95.Tn TCP 96port may still be specified 97at this time; if the port is not specified the system will assign one. 98Once a connection has been established the socket's address is 99fixed by the peer entity's location. The address assigned the 100socket is the address associated with the network interface 101through which packets are being transmitted and received. Normally 102this address corresponds to the peer entity's network. 103.Pp 104.Tn TCP 105supports one socket option which is set with 106.Xr setsockopt 2 107and tested with 108.Xr getsockopt 2 . 109Under most circumstances, 110.Tn TCP 111sends data when it is presented; 112when outstanding data has not yet been acknowledged, it gathers 113small amounts of output to be sent in a single packet once 114an acknowledgement is received. 115For a small number of clients, such as window systems 116that send a stream of mouse events which receive no replies, 117this packetization may cause significant delays. 118Therefore, 119.Tn TCP 120provides a boolean option, 121.Dv TCP_NODELAY 122(from 123.Aq Pa netinet/tcp.h , 124to defeat this algorithm. 125The option level for the 126.Xr setsockopt 127call is the protocol number for 128.Tn TCP , 129available from 130.Xr getprotobyname 3 . 131.Pp 132Options at the 133.Tn IP 134transport level may be used with 135.Tn TCP ; 136see 137.Xr ip 4 . 138Incoming connection requests that are source-routed are noted, 139and the reverse source route is used in responding. 140.Sh DIAGNOSTICS 141A socket operation may fail with one of the following errors returned: 142.Bl -tag -width [EADDRNOTAVAIL] 143.It Bq Er EISCONN 144when trying to establish a connection on a socket which 145already has one; 146.It Bq Er ENOBUFS 147when the system runs out of memory for 148an internal data structure; 149.It Bq Er ETIMEDOUT 150when a connection was dropped 151due to excessive retransmissions; 152.It Bq Er ECONNRESET 153when the remote peer 154forces the connection to be closed; 155.It Bq Er ECONNREFUSED 156when the remote 157peer actively refuses connection establishment (usually because 158no process is listening to the port); 159.It Bq Er EADDRINUSE 160when an attempt 161is made to create a socket with a port which has already been 162allocated; 163.It Bq Er EADDRNOTAVAIL 164when an attempt is made to create a 165socket with a network address for which no network interface 166exists. 167.El 168.Sh SEE ALSO 169.Xr getsockopt 2 , 170.Xr socket 2 , 171.Xr intro 4 , 172.Xr inet 4 , 173.Xr ip 4 174.Sh HISTORY 175The 176.Nm 177protocol stack appeared in 178.Bx 4.2 . 179