|
» |
|
|
|
NAMETCP — Internet Transmission Control Protocol SYNOPSIS#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
s = socket(AF_INET, SOCK_STREAM, 0);
s = socket(AF_INET6, SOCK_STREAM, 0); DESCRIPTIONThe
TCP
protocol provides reliable, flow-controlled,
two-way transmission of data.
It is a byte-stream protocol used to support the
SOCK_STREAM
socket type.
TCP
constructs virtual circuits between peer entities.
A virtual circuit consists of remote Internet addresses,
remote ports, local Internet addresses and local ports.
IP
uses the Internet addresses to direct messages between hosts,
and the port numbers to identify a
TCP
entity at a particular host. Sockets using
TCP
are either
active
or
passive.
connect()
creates active sockets, which initiate connections to passive sockets
(see
connect(2)).
To create a passive socket, use the
listen()
system call after binding the socket with the
bind()
system call (see
listen(2)
and
bind(2)).
Only passive sockets can use the
accept()
call to accept incoming connections (see
accept(2)). Passive sockets can
underspecify
their location to match incoming connection requests from multiple networks.
This technique, called
wildcard addressing,
allows a single server to provide service to clients on multiple networks.
To create a socket that listens on all networks, the Internet address
INADDR_ANY
must be bound for AF_INET family and
in6addr_any
for AF_INET6 family.
The
TCP
port can still be specified even if wildcard addressing is being used.
If the port is specified as zero, the system assigns a port. Once
accept()
has a rendezvous with a connect request,
a virtual circuit is established between peer entities.
bind()
supplies the local port and local Internet address and
accept()
gathers the remote port and remote Internet address from the peer requesting
the connection. OptionsThe system supports the following socket options:
TCP_MAXSEG,
TCP_NODELAY,
TCP_ABORT_THRESHOLD,
TCP_CONN_ABORT_THRESHOLD,
TCP_KEEPCNT,
TCP_KEEPIDLE,
TCP_KEEPINTVL,
TCP_TSOPTENA,
and
TCP_SACKENA
(defined in the include file
<netinet/tcp.h>).
The
TCP_MAXSEG
option can only be used with
getsockopt(),
while
TCP_NODELAY,
TCP_ABORT_THRESHOLD,
TCP_CONN_ABORT_THRESHOLD
TCP_KEEPCNT,
TCP_KEEPIDLE,
TCP_KEEPINTVL,
TCP_TSOPTENA,
and
TCP_SACKENA
can be set with
setsockopt()
and tested with
getsockopt()
(see
getsockopt(2)).
These options require
level
to be set to
IPPROTO_TCP
in the
getsockopt/setsockopt
call.
- TCP_MAXSEG
(non-boolean option) lets an application to receive the current segment size
of the TCP SOCK_STREAM socket.
The current segment size will be returned in
optval. - TCP_NODELAY
(boolean option) causes small amounts of output to be sent immediately. - TCP_ABORT_THRESHOLD
(non-boolean option) sets the second threshold timer for the connections
that are in ESTABLISHED state.
The option value is the threshold time in milliseconds. When it must retransmit packets because a timer has expired,
TCP first compares the total time it has waited against the two
thresholds, as described in RFC 1122, 4.2.3.5.
If it has waited longer than the second threshold (R2), TCP terminates
the connection.
The default value for this option is the current value of the ndd
tunable parameter
tcp_ip_abort_interval.
Refer to
ndd(1M)
online help for details on the
tcp_ip_abort_interval
default value. - TCP_CONN_ABORT_THRESHOLD
(non-boolean option) sets the second threshold timer during connection
establishment.
The option value is the threshold time in milliseconds. This option is the same as
TCP_ABORT_THRESHOLD,
except that this value is used during connection establishment.
When it must retransmit the SYN packet because a timer has expired,
TCP first compares the total time it has waited against the two thresholds.
If it has waited longer than the second threshold, TCP terminates the
connection.
The default value for this option is the current value of
the ndd tunable
tcp_ip_abort_cinterval.
See
ndd(1M)
online help for details on the
tcp_ip_abort_cinterval
default value. - TCP_KEEPCNT
(non-boolean option) When the
SO_KEEPALIVE
option is enabled, TCP probes a connection that has been
idle for some amount of time.
If the remote system does not respond to a keepalive probe, TCP
retransmits the probe a certain number of times
before a connection is considered to be broken.
The
TCP_KEEPCNT
option can be used to affect this value for a given socket, and specifies
the maximum number of keepalive probes to be sent.
This option takes an
int
value, with a range of 1 to 32767. - TCP_KEEPIDLE
(non-boolean option) When the
SO_KEEPALIVE
option is enabled, TCP probes a
connection that has been idle for some amount of time.
The default value for this idle period is 2 hours.
The
TCP_KEEPIDLE
option can be used to affect this value for a given socket, and specifies
the number of seconds of idle time between keepalive probes.
This option takes an
int
value, with a range of 1 to 32767. - TCP_KEEPINIT
(non-boolean option) If a TCP connection cannot be established within
some amount of time, TCP will time out the connect attempt.
The default value for this initial connection establishment timeout
is 75 seconds.
The
TCP_KEEPINIT
option can be used to affect this initial timeout period for a given socket,
and specifies the number of seconds to wait before the connect attempt is
timed out.
For passive connections, the
TCP_KEEPINIT
option value is inherited from the listening socket.
This option takes an
int
value, with a range of 1 to 32767. - TCP_KEEPINTVL
(non-boolean option) When the
SO_KEEPALIVE
option is enabled, TCP probes a connection that has been idle for some
amount of time.
If the remote system does not respond to a keepalive probe,
TCP retransmits the probe after some amount of time.
The default value for this retransmit interval is 75 seconds.
The
TCP_KEEPINTVL
option can be used to affect this value for a given socket, and specifies
the number of seconds to wait before retransmitting a keepalive probe.
This option takes an
int
value, with a range of 1 to 32767. - TCP_TSOPTENA
(boolean option) When this option is enabled, the sender places a timestamp
in each data segment.
The receiver, if configured to accept them, sends these timestamps back
in ACK segments.
This provides the sender with a mechanism with which to measure round-trip
time.
TCP provides a Boolean option,
TCP_TSOPTENA
(from the
<netinet/tcp.h>
header file) to enable or disable this option.
This option takes an
int
value.
When this option is enabled, the
TCP_PAWS
option is also enabled. - TCP_PAWS
(boolean option) When the PAWS (Protect Against Wrapped Sequence numbers)
option is enabled, the receiver rejects any old duplicate segments that are
received.
This option is used on synchronized TCP connections only.
TCP provides a Boolean option,
TCP_PAWS
(from the
<netinet/tcp.h>
header file) to enable or disable this option.
This option takes an
int
value.
This option automatically turns the
TCP_TSOPTENA
option on. - TCP_SACKENA
(boolean option) When the Selective Acknowledgment (SACK) option is enabled,
the data receiver can inform the sender about all segments that have arrived
successfully.
In this way, the sender need retransmit only those segments
that have actually been lost.
This option is useful in cases where multiple segments are dropped.
TCP provides a Boolean option,
TCP_SACKENA
(from the
<netinet/tcp.h>
header file) to enable or disable this option.
This option takes an
int
value.
If
TCP_NODELAY
is set, the system sends small amounts of output immediately
rather than gathering them into a single packet
after an acknowledgement is received.
If
TCP_NODELAY
is not set, the system sends data when it is presented,
if there is no outstanding unacknowledged data.
If there is outstanding unacknowledged data,
the system gathers small amounts of data
to be sent in a single packet once an acknowledgement is received.
For clients such as window managers
that send a stream of mouse events which receive no replies,
this packetization may cause significant delays.
The
TCP_NODELAY
option can be used to avoid this situation.
Note, however, that setting the
TCP_NODELAY
option may result in a large number of small packets
being sent over the network. By default,
TCP_NODELAY
is not set when a socket is created. The option level to use for accessing the
TCP
option with the
setsockopt()
or
getsockopt()
calls is the protocol number for
TCP
which is available from
getprotobyname()
(see
getprotoent(3N)). If the
SO_KEEPALIVE
socket option is enabled on an established TCP
connection and the connection has been idle for two hours,
TCP sends a packet to the remote socket, expecting the remote
TCP to acknowledge that the connection is still active. If the remote
TCP does not respond in a timely manner, TCP continues to send
keepalive packets according to its normal retransmission algorithm.
If the remote TCP does not respond within a particular time limit,
TCP drops the connection.
The next socket system call
(for example,
recv())
returns an error, and
errno
is set to
ETIMEDOUT.
See
getsockopt(2)
for details on enabling
SO_KEEPALIVE. The default send and receives buffer size is 32768 bytes
(see
WARNINGS
below).
The send and receive buffer sizes for
TCP
stream sockets can be altered by using the
SO_SNDBUF
and
SO_RCVBUF
options of the
setsockopt()
system call
or the
XTI_SNDBUF
and
XTI_RCVBUF
options of the
t_optmgmt()
system call.
Refer to
getsockopt(2)
or
t_optmgmt(3)
for details. The maximum transmit buffer size for a
TCP
stream socket is 2147483647 bytes. The maximum receive buffer
size for a
TCP
stream socket is 1073725440 bytes. These maximum values can be lowered
using the ndd variables
tcp_xmit_hiwater_max
and
tcp_recv_hiwater_max. ERRORSOne of the following errors may be returned in
errno
if a
socket operation fails.
For a more detailed list of errors, see the man pages for specific
system calls.
- EISCONN
The socket is already connected. - ENOBUFS
No buffer space is available for an internal data structure. - ETIMEDOUT
Connection dropped due to excessive retransmissions. - ECONNRESET
The connection was forcibly closed by the peer socket. - ECONNREFUSED
Remote peer actively refuses connection establishment
(usually because no process is listening to the port). - EADDRINUSE
The specified address is already in use. - EADDRNOTAVAIL
The specified address is not available on this machine.
WARNINGSThe default socket buffer size might increase without
notice in a future release or patch.
Therefore, if an application calls
setsockopt()
with
SO_RCVBUF,
it should do so before calling
listen(),
or it should first call
getsockopt()
with
SO_RCVBUF
and ensure that the intended new receive buffer
size is not less than the current buffer size.
These programming conventions are consistent with TCP
protocol restrictions against reducing the TCP
receive window after a connection has been established. AUTHORThe socket interfaces to TCP
were developed by the University of California, Berkeley. SEE ALSOndd(1M),
getsockopt(2),
recv(2),
send(2),
socket(2),
t_open(3),
t_optmgmt(3),
socket(7),
inet(7F). - RFC 793
Transmission Control Protocol - RFC 1122
Requirements for Internet hosts - RFC 1323
TCP Extensions for High Performance - RFC 1878
Variable Length Subnet Table for IPv4 - RFC 2018
TCP Selective Acknowledgement Options - RFC 2414
Increasing TCP's Initial Window - RFC 2582
NewReno Modifications to TCP's Fast Recovery Algorithm
|