socket.2 (2010 09)
s
socket(2) socket(2)
NAME
socket() - create an endpoint for communication
SYNOPSIS
#include <sys/socket.h>
AF_CCITT Only
#include <x25/x25ccittproto.h>
int socket(int af, int type, int protocol);
DESCRIPTION
The socket() system call creates an endpoint for communication and returns a descriptor. The socket
descriptor returned is used in all subsequent socket-related system calls.
The af parameter specifies an address family to be used to interpret addresses in later operations that
specify the socket. These address families are defined in the include files
<sys/socket.h>
and
<x25/ccittproto.h>
. The only currently supported address families are:
AF_INET (DARPA Internet addresses)
AF_INET6 (Internet Protocol version 6)
AF_ROUTE (routing messages followed by IPv4 or IPv6 addresses)
AF_UNIX (path names on a local node)
AF_CCITT (CCITT X.25 addresses)
AF_VME_LINK (backplane communications on VMEbus)
The type specifies the semantics of communication for the socket. Currently defined types are:
SOCK_STREAM Sequenced, reliable, two-way-connection-based byte streams.
SOCK_DGRAM Datagrams (connectionless, unreliable messages of a fixed, typically small,
maximum length).
protocol specifies a particular protocol to be used with the socket. Normally, only a single protocol exists
to support a particular socket type using a given address family. However, many protocols may exist, in
which case a particular protocol must be specified. The protocol number to use depends on the commun-
ication domain in which communication is to take place (see services (4) and protocols (4)). protocol can
be specified as zero, which causes the system to choose a protocol type to use.
Sockets of type SOCK_STREAM are byte streams similar to pipes, except that they are full-duplex
instead of half-duplex. A stream socket must be in a connected state before any data can be sent or
received on it. A connection to another socket is created with a
connect() or accept() call. Once
connected, data can be transferred using some variant of the send() and recv() or the read()
and
write() calls. When a session is complete, use close() or shutdown() calls to terminate the con-
nection.
TCP, the communications protocol used to implement SOCK_STREAM for AF_INET or AF_INET6 sock-
ets, ensures that data is not lost or duplicated. If a peer has buffer space for data and the data cannot be
successfully transmitted within a reasonable length of time, the connection is considered broken and the
next
recv() call indicates an error with errno set to [ETIMEDOUT]. If SO_KEEPALIVE is set and
the connection has been idle for two hours, the TCP protocol sends "keepalive" packets every 75 seconds
to determine whether the connection is active. These transmissions are not visible to users and cannot be
read by a recv() call. If the remote system does not respond within 10 minutes (after 8 "keepalive"
packets have been sent), the next socket call (recv()) returns an error with errno set to
[ETIMEDOUT]. A SIGPIPE signal is raised if a process sends on a broken stream. This causes naive
processes that do not handle the signal to exit. An end-of-file condition (zero bytes read) is returned if a
process tries to read on a broken stream.
SOCK_DGRAM sockets allow sending of messages to correspondents named in
send() calls. It is also
possible to receive messages at such a socket with recv().
The operation of sockets is controlled by socket level options set by the
setsockopt() system call
described by the getsockopt (2) manual entry. These options are defined in the file <sys/socket.h>
and explained in the getsockopt (2) manual entry.
X.25 Only
Socket endpoints for communication over an X.25/9000 link can be in either address family, AF_INET or
AF_CCITT. If the socket is in the AF_INET family, the connection behaves as described above. TCP is
HP-UX 11i Version 3: September 2010 − 1 − Hewlett-Packard Company 1