Professional Documents
Culture Documents
NAME
socket − create an endpoint for communication
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
Socket creates an endpoint for communication and returns a
descriptor.
SOCK_STREAM
Provides sequenced, reliable, two−way, connection−
based byte streams. An out−of−band data transmis-
sion mechanism may be supported.
SOCK_DGRAM
Supports datagrams (connectionless, unreliable mes-
sages of a fixed maximum length).
SOCK_SEQPACKET
Provides a sequenced, reliable, two−way connection−
based data transmission path for datagrams of fixed
maximum length; a consumer is required to read an
entire packet with each read system call.
SOCK_RAW
Provides raw network protocol access.
SOCK_RDM
Provides a reliable datagram layer that does not
guarantee ordering.
SOCK_PACKET
Obsolete and should not be used in new programs;
see packet(7).
RETURN VALUE
−1 is returned if an error occurs; otherwise the return
value is a descriptor referencing the socket.
ERRORS
EPROTONOSUPPORT
The protocol type or the specified protocol is not
supported within this domain.
ENOBUFS or ENOMEM
Insufficient memory is available. The socket can-
not be created until sufficient resources are
freed.
CONFORMING TO
4.4BSD (the socket function call appeared in 4.2BSD). Gen-
erally portable to/from non−BSD systems supporting clones
of the BSD socket layer (including System V variants).
NOTE
The manifest constants used under BSD 4.* for protocol
families are PF_UNIX, PF_INET, etc., while AF_UNIX etc.
are used for address families. However, already the BSD
man page promises: "The protocol family generally is the
same as the address family", and subsequent standards use
AF_* everywhere.
BUGS
SOCK_UUCP is not implemented yet.
SEE ALSO
accept(2), bind(2), connect(2), getprotoent(3), getsock-
name(2), getsockopt(2), ioctl(2), listen(2), read(2),
recv(2), select(2), send(2), shutdown(2), socketpair(2),
write(2)
NAME
bind − bind a name to a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
bind gives the socket sockfd the local address my_addr.
my_addr is addrlen bytes long. Traditionally, this is
called "assigning a name to a socket." When a socket is
created with socket(2), it exists in a name space (address
family) but has no name assigned.
NOTES
The rules used in name binding vary between address fami-
lies. Consult the manual entries in Section 7 for
detailed information. For AF_INET see ip(7), for AF_UNIX
see unix(7), for AF_APPLETALK see ddp(7), for AF_PACKET
see packet(7), for AF_X25 see x25(7) and for AF_NETLINK
see netlink(7).
RETURN VALUE
On success, zero is returned. On error, −1 is returned,
and errno is set appropriately.
ERRORS
EBADF sockfd is not a valid descriptor.
ENOTSOCK
Argument is a descriptor for a file, not a socket.
system.
ENAMETOOLONG
my_addr is too long.
ENOTDIR
A component of the path prefix is not a directory.
BUGS
The transparent proxy options are not described.
CONFORMING TO
SVr4, 4.4BSD (the bind function first appeared in BSD
4.2). SVr4 documents additional EADDRNOTAVAIL, EADDRI-
NUSE, and ENOSR general error conditions, and additional
EIO, EISDIR and EROFS Unix−domain error conditions.
NOTE
The third argument of bind is in reality an int (and this
is what BSD 4.* and libc4 and libc5 have). Some POSIX
confusion resulted in the present socklen_t. The draft
standard has not been adopted yet, but glibc2 already fol-
lows it and also has socklen_t. See also accept(2).
SEE ALSO
accept(2), connect(2), listen(2), socket(2), getsock-
name(2), ip(7), socket(7)
NAME
listen − listen for connections on a socket
SYNOPSIS
#include <sys/socket.h>
DESCRIPTION
To accept connections, a socket is first created with
socket(2), a willingness to accept incoming connections
and a queue limit for incoming connections are specified
with listen, and then the connections are accepted with
accept(2). The listen call applies only to sockets of
type SOCK_STREAM or SOCK_SEQPACKET.
NOTES
The behaviour of the backlog parameter on TCP sockets
changed with Linux 2.2. Now it specifies the queue length
for completely established sockets waiting to be accepted,
instead of the number of incomplete connection requests.
The maximum length of the queue for incomplete sockets can
be set using the tcp_max_syn_backlog sysctl. When syn-
cookies are enabled there is no logical maximum length and
this sysctl setting is ignored. See tcp(7) for more
information.
RETURN VALUE
On success, zero is returned. On error, −1 is returned,
and errno is set appropriately.
ERRORS
EBADF The argument s is not a valid descriptor.
ENOTSOCK
The argument s is not a socket.
EOPNOTSUPP
The socket is not of a type that supports the lis-
ten operation.
CONFORMING TO
Single Unix, 4.4BSD, POSIX 1003.1g draft. The listen func-
tion call first appeared in 4.2BSD.
BUGS
If the socket is of type AF_INET, and the backlog argument
is greater than the constant SOMAXCONN (128 in Linux 2.0 &
2.2), it is silently truncated to SOMAXCONN. Don’t rely
on this value in portable applications since BSD (and some
BSD−derived systems) limit the backlog to 5.
SEE ALSO
accept(2), connect(2), socket(2)
NAME
accept − accept a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
The accept function is used with connection−based socket
types (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It
extracts the first connection request on the queue of
pending connections, creates a new connected socket with
mostly the same properties as s, and allocates a new file
descriptor for the socket, which is returned. The newly
created socket is no longer in the listening state. The
original socket s is unaffected by this call. Note that
any per file descriptor flags (everything that can be set
with the F_SETFL fcntl, like non blocking or async state)
are not inherited across a accept.
NOTES
There may not always be a connection waiting after a SIGIO
is delivered or select(2) or poll(2) return a readability
event because the connection might have been removed by an
asynchronous network error or another thread before accept
is called. If this happens then the call will block wait-
ing for the next connection to arrive. To ensure that
accept never blocks, the passed socket s needs to have the
O_NONBLOCK flag set (see socket(7)).
RETURN VALUE
The call returns −1 on error. If it succeeds, it returns
a non−negative integer that is a descriptor for the
accepted socket.
ERROR HANDLING
Linux accept passes already−pending network errors on the
new socket as an error code from accept. This behaviour
differs from other BSD socket implementations. For reli-
able operation the application should detect the network
errors defined for the protocol after accept and treat
them like EAGAIN by retrying. In case of TCP/IP these are
ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUN-
REACH, EOPNOTSUPP, and ENETUNREACH.
ERRORS
EAGAIN or EWOULDBLOCK
The socket is marked non−blocking and no connec-
tions are present to be accepted.
ENOTSOCK
The descriptor references a file, not a socket.
EOPNOTSUPP
The referenced socket is not of type SOCK_STREAM.
ENOBUFS, ENOMEM
Not enough free memory. This often means that the
memory allocation is limited by the socket buffer
CONFORMING TO
SVr4, 4.4BSD (the accept function first appeared in BSD
4.2). The BSD man page documents five possible error
returns (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK,
EFAULT). SUSv2 documents errors EAGAIN, EBADF,
ECONNABORTED, EFAULT, EINTR, EINVAL, EMFILE, ENFILE,
ENOBUFS, ENOMEM, ENOSR, ENOTSOCK, EOPNOTSUPP, EPROTO,
EWOULDBLOCK.
NOTE
The third argument of accept was originally declared as an
‘int *’ (and is that under libc4 and libc5 and on many
other systems like BSD 4.*, SunOS 4, SGI); a POSIX 1003.1g
draft standard wanted to change it into a ‘size_t *’, and
that is what it is for SunOS 5. Later POSIX drafts have
‘socklen_t *’, and so do the Single Unix Specification and
glibc2. Quoting Linus Torvalds: _Any_ sane library _must_
have "socklen_t" be the same size as int. Anything else
breaks any BSD socket layer stuff. POSIX initially _did_
make it a size_t, and I (and hopefully others, but obvi-
ously not too many) complained to them very loudly indeed.
Making it a size_t is completely broken, exactly because
size_t very seldom is the same size as "int" on 64−bit
architectures, for example. And it _has_ to be the same
size as "int" because that’s what the BSD socket interface
is. Anyway, the POSIX people eventually got a clue, and
created "socklen_t". They shouldn’t have touched it in
the first place, but once they did they felt it had to
have a named type for some unfathomable reason (probably
somebody didn’t like losing face over having done the
original stupid thing, so they silently just renamed their
blunder).
SEE ALSO
bind(2), connect(2), listen(2), select(2), socket(2)
NAME
connect − initiate a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
The file descriptor sockfd must refer to a socket. If the
socket is of type SOCK_DGRAM then the serv_addr address is
the address to which datagrams are sent by default, and
the only address from which datagrams are received. If
the socket is of type SOCK_STREAM or SOCK_SEQPACKET, this
call attempts to make a connection to another socket. The
other socket is specified by serv_addr, which is an
address (of length addrlen) in the communications space of
the socket. Each communications space interprets the
serv_addr parameter in its own way.
RETURN VALUE
If the connection or binding succeeds, zero is returned.
On error, −1 is returned, and errno is set appropriately.
ERRORS
The following are general socket errors only. There may
be other domain−specific error codes.
ENOTSOCK
The file descriptor is not associated with a
socket.
EISCONN
The socket is already connected.
ECONNREFUSED
No one listening on the remote address.
ETIMEDOUT
Timeout while attempting connection. The server may
be too busy to accept new connections. Note that
for IP sockets the timeout may be very long when
syncookies are enabled on the server.
ENETUNREACH
Network is unreachable.
EADDRINUSE
Local address is already in use.
EINPROGRESS
The socket is non−blocking and the connection can-
not be completed immediately. It is possible to
select(2) or poll(2) for completion by selecting
the socket for writing. After select indicates
writability, use getsockopt(2) to read the SO_ERROR
option at level SOL_SOCKET to determine whether
connect completed successfully (SO_ERROR is zero)
or unsuccessfully (SO_ERROR is one of the usual
error codes listed here, explaining the reason for
the failure).
EALREADY
The socket is non−blocking and a previous connec-
tion attempt has not yet been completed.
EAFNOSUPPORT
The passed address didn’t have the correct address
family in its sa_family field.
EACCES, EPERM
The user tried to connect to a broadcast address
without having the socket broadcast flag enabled or
the connection request failed because of a local
firewall rule.
CONFORMING TO
SVr4, 4.4BSD (the connect function first appeared in BSD
4.2). SVr4 documents the additional general error codes
EADDRNOTAVAIL, EINVAL, EAFNOSUPPORT, EALREADY, EINTR,
EPROTOTYPE, and ENOSR. It also documents many additional
error conditions not described here.
NOTE
The third argument of connect is in reality an int (and
this is what BSD 4.* and libc4 and libc5 have). Some
POSIX confusion resulted in the present socklen_t. The
BUGS
Unconnecting a socket by calling connect with a AF_UNSPEC
address is not yet implemented.
SEE ALSO
accept(2), bind(2), listen(2), socket(2), getsockname(2)
NAME
recv, recvfrom, recvmsg − receive a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
The recvfrom and recvmsg calls are used to receive mes-
sages from a socket, and may be used to receive data on a
socket whether or not it is connection−oriented.
MSG_OOB
This flag requests receipt of out−of−band data that
would not be received in the normal data stream.
Some protocols place expedited data at the head of
MSG_PEEK
This flag causes the receive operation to return
data from the beginning of the receive queue with-
out removing that data from the queue. Thus, a
subsequent receive call will return the same data.
MSG_WAITALL
This flag requests that the operation block until
the full request is satisfied. However, the call
may still return less data than requested if a sig-
nal is caught, an error or disconnect occurs, or
the next data to be received is of a different type
than that returned.
MSG_NOSIGNAL
This flag turns off raising of SIGPIPE on stream
sockets when the other end disappears.
MSG_ERRQUEUE
This flag specifies that queued errors should be
received from the socket error queue. The error is
passed in an ancillary message with a type depen-
dent on the protocol (for IPv4 IP_RECVERR ). The
user should supply a buffer of sufficient size.
See cmsg(3) for more information on ancillary mes-
sages.
#define SO_EE_ORIGIN_NONE 0
#define SO_EE_ORIGIN_LOCAL 1
#define SO_EE_ORIGIN_ICMP 2
#define SO_EE_ORIGIN_ICMP6 3
struct sock_extended_err
{
u_int32_t ee_errno; /* error number */
u_int8_t ee_origin; /* where the error originated
*/
u_int8_t ee_type; /* type */
u_int8_t ee_code; /* code */
u_int8_t ee_pad;
u_int32_t ee_info; /* additional information */
u_int32_t ee_data; /* other data */
/* More data may follow */
};
struct msghdr {
void * msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec * msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void * msg_control; /* ancillary data, see below
*/
socklen_t msg_controllen; /* ancillary data buffer len
*/
int msg_flags; /* flags on received message
*/
};
struct cmsghdr {
socklen_t cmsg_len; /* data byte count, including hdr
*/
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol−specific type */
/* followed by
u_char cmsg_data[]; */
};
Linux Man Page Apr 1999 3
RETURN VALUES
These calls return the number of bytes received, or −1 if
an error occurred.
ERRORS
These are some standard errors generated by the socket
layer. Additional errors may be generated and returned
from the underlying protocol modules; see their manual
pages.
ECONNREFUSED
A remote host refused to allow the network connec-
tion (typically because it is not running the
requested service).
ENOTCONN
The socket is associated with a connection−oriented
protocol and has not been connected (see connect(2)
and accept(2)).
ENOTSOCK
The argument s does not refer to a socket.
CONFORMING TO
4.4BSD (these function calls first appeared in 4.2BSD).
NOTE
The prototypes given above follow glibc2. The Single Unix
Specification agrees, except that it has return values of
type ‘ssize_t’ (while BSD 4.* and libc4 and libc5 all have
‘int’). The flags argument is ‘int’ in BSD 4.*, but
‘unsigned int’ in libc4 and libc5. The len argument is
‘int’ in BSD 4.*, but ‘size_t’ in libc4 and libc5. The
fromlen argument is ‘int *’ in BSD 4.*, libc4 and libc5.
The present ‘socklen_t *’ was invented by POSIX. See
also accept(2).
SEE ALSO
fcntl(2), read(2), select(2), getsockopt(2), socket(2),
cmsg(3)
Linux Man Page Apr 1999 5
NAME
send, sendto, sendmsg − send a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
Send, sendto, and sendmsg are used to transmit a message
to another socket. Send may be used only when the socket
is in a connected state, while sendto and sendmsg may be
used at any time.
When the message does not fit into the send buffer of the
socket, send normally blocks, unless the socket has been
placed in non−blocking I/O mode. In non−blocking mode it
would return EAGAIN in this case. The select(2) call may
be used to determine when it is possible to send more
data.
MSG_OOB
Sends out−of−band data on sockets that support this
notion (e.g. SOCK_STREAM); the underlying protocol
must also support out−of−band data.
MSG_DONTROUTE
Dont’t use a gateway to send out the packet, only
send to hosts on directly connected networks. This
is usually used only by diagnostic or routing pro-
grams. This is only defined for protocol families
that route; packet sockets don’t.
MSG_DONTWAIT
Enables non−blocking operation; if the operation
would block, EAGAIN is returned (this can also be
enabled using the O_NONBLOCK with the F_SETFL
Linux Man Page July 1999 1
fcntl(2)).
MSG_NOSIGNAL
Requests not to send SIGPIPE on errors on stream
oriented sockets when the other end breaks the con-
nection. The EPIPE error is still returned.
RETURN VALUES
The calls return the number of characters sent, or −1 if
an error occurred.
ERRORS
These are some standard errors generated by the socket
layer. Additional errors may be generated and returned
from the underlying protocol modules; see their respective
manual pages.
ENOTSOCK
The argument s is not a socket.
EMSGSIZE
The socket requires that message be sent atomi-
cally, and the size of the message to be sent made
this impossible.
EAGAIN or EWOULDBLOCK
The socket is marked non−blocking and the requested
operation would block.
ENOBUFS
The output queue for a network interface was full.
This generally indicates that the interface has
stopped sending, but may be caused by transient
congestion. (This cannot occur in Linux, packets
are just silently dropped when a device queue over-
flows.)
CONFORMING TO
4.4BSD, SVr4, POSIX 1003.1g draft (these function calls
appeared in 4.2BSD).
NOTE
The prototypes given above follow the Single Unix Specifi-
cation, as glibc2 also does; the flags argument was ‘int’
in BSD 4.*, but ‘unsigned int’ in libc4 and libc5; the len
argument was ‘int’ in BSD 4.* and libc4, but ‘size_t’ in
libc5; the tolen argument was ‘int’ in BSD 4.* and libc4
and libc5. See also accept(2).
SEE ALSO
fcntl(2), recv(2), select(2), getsockopt(2), sendfile(2),
socket(2), write(2), socket(7), ip(7), tcp(7), udp(7)
Linux Man Page July 1999 3