'\"macro stdmacro
.if n .pH g7.tcp @(#)tcp	40.6 of 10/10/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} TCP 7 "TCP/IP" "\&"
.if \nX=1 .ds x} TCP 7 "TCP/IP"
.if \nX=2 .ds x} TCP 7 "" "\&"
.if \nX=3 .ds x} TCP "" "" "\&"
.TH \*(x}
.SH NAME
TCP \- Internet Transmission Control Protocol
.SH SYNOPSIS
.nf
\f4#include <sys/socket.h>\f1
\f4#include <netinet/in.h>\f1
.LP
\f4s = socket(AF_INET, SOCK_STREAM, 0);\f1
.LP
\f4t = t_open("/dev/tcp", O_RDWR);\f1
.fi
.SH DESCRIPTION
.LP
.SM TCP
is the virtual circuit protocol of the Internet protocol family.
It provides reliable, flow-controlled, in order,
two-way transmission of data.
It is a byte-stream protocol
layered above the Internet Protocol (\s-1IP\s0),
the Internet protocol family's
internetwork datagram delivery protocol.
.LP
Programs can access 
.SM TCP
using the socket interface as a
\f4SOCK_STREAM\f1
socket type, or using the Transport Level Interface
(\s-1TLI\s0) 
where it supports the
connection-oriented
(\f4T_COTS_ORD\f1)
service type.
.LP
.SM TCP
uses
.SM IP\s0's
host-level addressing and
adds its own per-host collection of \(lqport addresses.\(rq
The endpoints of a
.SM TCP
connection are identified by the
combination of an
.SM IP
address and a
.SM TCP
port number.
Although other protocols, such as the User Datagram Protocol (\s-1UDP\s0),
may use the same host and port address format,
the port space of these protocols is distinct.
See
\f4inet\f1(7)
for details on the common aspects of addressing in the Internet
protocol family.
.LP
Sockets utilizing
.SM TCP
are either \(lqactive\(rq or
\(lqpassive\(rq.
Active sockets initiate connections to passive sockets.
Both types of sockets must have their local
.SM IP
address and
.SM TCP
port number bound with the
\f4bind\f1(2)
system call after the socket is created.
By default,
.SM TCP
sockets are active.
A passive socket is created by calling the
\f4listen\f1(2)
system call after binding the socket with
\f4bind(\|)\f1.
This establishes a queueing parameter for the passive socket.
After this, connections to the passive socket can be received with the
\f4accept\f1(2)
system call.
Active sockets use the
\f4connect\f1(2)
call after binding to initiate connections.
.LP
By using the special value
\f4INADDR_ANY\f1,
the local
.SM IP
address can be left unspecified in the
\f4bind(\|)\f1
call by either active or passive
.SM TCP
sockets.
This feature is usually used if
the local address is either unknown or irrelevant.
If left unspecified,
the local
.SM IP
address will be bound at connection
time to the address of the network interface used to
service the connection.
.LP
Once a connection has been established,
data can be exchanged using the
\f4read\f1(2)
and
\f4write\f1(2)
system calls.
.LP
.SM TCP
supports one socket option which is set with
\f4setsockopt(\|)\f1
and tested with
\f4getsockopt\f1(2).
Under most circumstances,
.SM TCP
sends data when it is presented.
When outstanding data has not yet been acknowledged, it gathers
small amounts of output to be sent in a single packet once
an acknowledgement is received.
For a small number of clients, such as window systems
that send a stream of mouse events which receive no replies,
this packetization may cause significant delays.
Therefore,
.SM TCP
provides a boolean option,
\f4TCP_NODELAY\f1
(defined in
\f4/usr/include/netinet/tcp.h\f1),
to defeat this algorithm.
The option level for the
\f4setsockopt(\|)\f1
call is the protocol number for
.SM TCP\s0,
available from
\f4getprotobyname(\|)\f1
[see
\f4getprotoent\f1(3N)].
.LP
Options at the
.SM IP
level may be used with
.SM TCP\s0;
See
\f4ip\f1(7).
.LP
.SM TCP
provides an urgent data mechanism,
which may be invoked using the out-of-band provisions of
\f4send\f1(2).
The caller may mark one byte as \(lqurgent\(rq with the
\f4MSG_OOB\f1
flag to
\f4send\f1(2).
This sets an \(lqurgent pointer\(rq pointing to this byte
in the
.SM TCP
stream.
The receiver on the other side of the stream is notified
of the urgent data by a
\f4SIGURG\f1
signal.
The
\f4SIOCATMARK\f1
\f4ioctl(\|)\f1
request returns a value indicating whether the stream
is at the urgent mark.
Because the system never returns data across the urgent mark
in a single
\f4read\f1(2)
call,
it is possible to advance to the urgent data in a simple loop
which reads data,
testing the socket with the
\f4SIOCATMARK ioctl(\|)\f1
request,
until it reaches the mark.
.LP
Incoming connection requests that include an
.SM IP
source route option are noted,
and the reverse source route is used in responding.
.br
.ne 6
.LP
A checksum over all data helps
.SM TCP
implement reliability.
Using a window-based flow control mechanism that
makes use of positive acknowledgements, sequence numbers, and
a retransmission strategy,
.SM TCP
can usually recover when datagrams are damaged,
delayed, duplicated or delivered
out of order by the underlying communication medium.
.LP
If the local
.SM TCP
receives no acknowledgements from its peer for
a period of time,
as would be the case if the remote machine crashed,
the connection is closed and an error is returned to the user.
If the remote machine reboots or otherwise loses state information
about a
.SM TCP
connection,
the connection is aborted and an error is returned to the user.
.SH SEE ALSO
\f4read\f1(2),
\f4write\f1(2),
\f4accept\f1(3N),
\f4bind\f1(3N),
\f4connect\f1(3N),
\f4getprotoent\f1(3N),
\f4getsockopt\f1(3N),
\f4listen\f1(3N),
\f4send\f1(3N),
\f4inet\f1(7),
\f4ip\f1(7).
.LP
Postel, Jon,
.IR "Transmission Control Protocol - \s-1DARPA\s0 Internet Program Protocol Specification" ,
.SM RFC
793,
Network Information Center,
.SM SRI
International, Menlo Park, Calif.,
September 1981.
.SH DIAGNOSTICS
.LP
A socket operation may fail if:
.TP 20
\f4EISCONN\f1
A
\f4connect(\|)\f1
operation was attempted on a socket on which a
\f4connect(\|)\f1
operation had already been performed.
.TP
\f4ETIMEDOUT\f1
A connection was dropped
due to excessive retransmissions.
.TP
\f4ECONNRESET\f1
The remote peer
forced the connection to be closed (usually because the remote machine
has lost state information about the connection due to a crash).
.TP
\f4ECONNREFUSED\f1
The remote
peer actively refused connection establishment (usually because
no process is listening to the port).
.TP
\f4EADDRINUSE\f1
A
\f4bind(\|)\f1
operation was attempted on a socket with a network address/port pair that has
already been bound to another socket.
.TP
\f4EADDRNOTAVAIL\f1
A
\f4bind(\|)\f1
operation was attempted on a socket with a network address
for which no network interface exists.
.TP 20
\f4EACCES\f1
A
\f4bind(\|)\f1
operation was attempted with a \(lqreserved\(rq port number and the effective user
.SM ID
of the process was not the privileged user.
.TP
\f4ENOBUFS\f1
The system ran out of memory for internal data structures.
