1*61599Sbostic.\" Copyright (c) 1983, 1991, 1993 2*61599Sbostic.\" The Regents of the University of California. All rights reserved. 320744Smckusick.\" 443581Strent.\" %sccs.include.redist.man% 520744Smckusick.\" 6*61599Sbostic.\" @(#)tcp.4 8.1 (Berkeley) 06/05/93 736752Sbostic.\" 847675Scael.Dd 947675Scael.Dt TCP 4 1047675Scael.Os BSD 4.2 1147675Scael.Sh NAME 1247675Scael.Nm tcp 1347675Scael.Nd Internet Transmission Control Protocol 1447675Scael.Sh SYNOPSIS 1547675Scael.Fd #include <sys/socket.h> 1647675Scael.Fd #include <netinet/in.h> 1747675Scael.Ft int 1847675Scael.Fn socket AF_INET SOCK_STREAM 0 1947675Scael.Sh DESCRIPTION 2047675ScaelThe 2147675Scael.Tn TCP 2247675Scaelprotocol provides reliable, flow-controlled, two-way 2320744Smckusicktransmission of data. It is a byte-stream protocol used to 2447675Scaelsupport the 2547675Scael.Dv SOCK_STREAM 2647675Scaelabstraction. TCP uses the standard 2720744SmckusickInternet address format and, in addition, provides a per-host 2847675Scaelcollection of 2947675Scael.Dq port addresses . 3047675ScaelThus, each address is composed 3120744Smckusickof an Internet address specifying the host and network, with 3247675Scaela specific 3347675Scael.Tn TCP 3447675Scaelport on the host identifying the peer entity. 3547675Scael.Pp 3647675ScaelSockets utilizing the tcp protocol are either 3747675Scael.Dq active 3847675Scaelor 3947675Scael.Dq passive . 4047675ScaelActive sockets initiate connections to passive 4147675Scaelsockets. By default 4247675Scael.Tn TCP 4347675Scaelsockets are created active; to create a 4420744Smckusickpassive socket the 4547675Scael.Xr listen 2 4620744Smckusicksystem call must be used 4720744Smckusickafter binding the socket with the 4847675Scael.Xr bind 2 4920744Smckusicksystem call. Only 5020744Smckusickpassive sockets may use the 5147675Scael.Xr accept 2 5220744Smckusickcall to accept incoming connections. Only active sockets may 5320744Smckusickuse the 5447675Scael.Xr connect 2 5520744Smckusickcall to initiate connections. 5647675Scael.Pp 5747675ScaelPassive sockets may 5847675Scael.Dq underspecify 5947675Scaeltheir location to match 6020744Smckusickincoming connection requests from multiple networks. This 6147675Scaeltechnique, termed 6247675Scael.Dq wildcard addressing , 6347675Scaelallows a single 6420744Smckusickserver to provide service to clients on multiple networks. 6520744SmckusickTo create a socket which listens on all networks, the Internet 6647675Scaeladdress 6747675Scael.Dv INADDR_ANY 6847675Scaelmust be bound. The 6947675Scael.Tn TCP 7047675Scaelport may still be specified 7120744Smckusickat this time; if the port is not specified the system will assign one. 7220744SmckusickOnce a connection has been established the socket's address is 7320744Smckusickfixed by the peer entity's location. The address assigned the 7420744Smckusicksocket is the address associated with the network interface 7520744Smckusickthrough which packets are being transmitted and received. Normally 7620744Smckusickthis address corresponds to the peer entity's network. 7747675Scael.Pp 7847675Scael.Tn TCP 7947675Scaelsupports one socket option which is set with 8047675Scael.Xr setsockopt 2 8128252Skarelsand tested with 8247675Scael.Xr getsockopt 2 . 8347675ScaelUnder most circumstances, 8447675Scael.Tn TCP 8547675Scaelsends data when it is presented; 8628252Skarelswhen outstanding data has not yet been acknowledged, it gathers 8728252Skarelssmall amounts of output to be sent in a single packet once 8828252Skarelsan acknowledgement is received. 8928252SkarelsFor a small number of clients, such as window systems 9028252Skarelsthat send a stream of mouse events which receive no replies, 9128252Skarelsthis packetization may cause significant delays. 9247675ScaelTherefore, 9347675Scael.Tn TCP 9447675Scaelprovides a boolean option, 9547675Scael.Dv TCP_NODELAY 9647675Scael(from 9747675Scael.Aq Pa netinet/tcp.h , 9828252Skarelsto defeat this algorithm. 9928252SkarelsThe option level for the 10047675Scael.Xr setsockopt 10147675Scaelcall is the protocol number for 10247675Scael.Tn TCP , 10328252Skarelsavailable from 10447675Scael.Xr getprotobyname 3 . 10547675Scael.Pp 10647675ScaelOptions at the 10747675Scael.Tn IP 10847675Scaeltransport level may be used with 10947675Scael.Tn TCP ; 11047675Scaelsee 11147675Scael.Xr ip 4 . 11228252SkarelsIncoming connection requests that are source-routed are noted, 11328252Skarelsand the reverse source route is used in responding. 11447675Scael.Sh DIAGNOSTICS 11520744SmckusickA socket operation may fail with one of the following errors returned: 11647675Scael.Bl -tag -width [EADDRNOTAVAIL] 11747675Scael.It Bq Er EISCONN 11820744Smckusickwhen trying to establish a connection on a socket which 11920744Smckusickalready has one; 12047675Scael.It Bq Er ENOBUFS 12120744Smckusickwhen the system runs out of memory for 12220744Smckusickan internal data structure; 12347675Scael.It Bq Er ETIMEDOUT 12420744Smckusickwhen a connection was dropped 12520744Smckusickdue to excessive retransmissions; 12647675Scael.It Bq Er ECONNRESET 12720744Smckusickwhen the remote peer 12820744Smckusickforces the connection to be closed; 12947675Scael.It Bq Er ECONNREFUSED 13020744Smckusickwhen the remote 13120744Smckusickpeer actively refuses connection establishment (usually because 13220744Smckusickno process is listening to the port); 13347675Scael.It Bq Er EADDRINUSE 13420744Smckusickwhen an attempt 13520744Smckusickis made to create a socket with a port which has already been 13620744Smckusickallocated; 13747675Scael.It Bq Er EADDRNOTAVAIL 13820744Smckusickwhen an attempt is made to create a 13920744Smckusicksocket with a network address for which no network interface 14020744Smckusickexists. 14147675Scael.El 14247675Scael.Sh SEE ALSO 14347675Scael.Xr getsockopt 2 , 14447675Scael.Xr socket 2 , 14547675Scael.Xr intro 4 , 14647675Scael.Xr inet 4 , 14747675Scael.Xr ip 4 14847675Scael.Sh HISTORY 14947675ScaelThe 15047675Scael.Nm 15147675Scaelprotocol stack appeared in 15247675Scael.Bx 4.2 . 153