socket(2)
Requires Optional LAN/X.25 Software
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
socket 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_UNIX (path names on a local node)
AF_CCITT ( CCITT X.25 addresses)
The type specifies the semantics of communication for the socket. Currently defined types are:
SOCK_STREAM
SOCK_DGRAM (for AF_INET only)
A SOCK_STREAM type provides sequenced, reliable, two-way-connection-based byte streams. A SOCK_DGRAM socket supports 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 communication domain in which communication is to take place (see services(4) and protocols(4)). protocol can be supplied as zero, in which case the system chooses 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(2) or accept(2) call. Once connected, data can be transferred using some variant of the send(2) and recv(2) or the read(2) and write(2) calls. When a session has been completed, a close(2) can be performed.
TCP, the communications protocol used to implement SOCK_STREAM for AF_INET sockets, 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(2) call indicates an error with errno set to ETIMEDOUT. If SO_KEEPALIVE is set, the TCP protocol keeps otherwise inactive socket connections active by forcing transmissions every 45 seconds. These transmissions are not visible to users, and cannot be read by a recv(2) call. An error is indicated if no response can be elicited on an otherwise idle connection for a extended period (e.g., 6 minutes). 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(2) calls. It is also possible to receive messages at such a socket with recv(2).
The operation of sockets is controlled by socket level options set by the setsockopt system call described by the getsockopt(2) reference page. These options are defined in the file <sys/socket.h> and explained in the getsockopt (2) reference page.
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 will behave as described above. TCP is used if the socket type is SOCK_STREAM; UDP is used if the socket type is SOCK_DGRAM. In both cases, Internet Protocol (IP) and the X.25-to- IP interface module are used. If the socket is in the AF_CCITT address family, only the SOCK_STREAM socket type is supported. Refer to the topic Comparing X.25 Level 3 Access to IP in the X.25 Programmer’s Guide for more details on the difference between programmatic access to X.25 via IP and X.25 Level 3.
If the socket is of the AF_CCITT family, the connection and all other operations pass data directly from the application to the X.25 Packet Level (level 3) without passing through a TCP or UDP protocol. Connections of the AF_CCITT family cannot use most of the socket level options described in the getsockopt(2) manual reference page. However, AF_CCITT connections can use many X.25-specific ioctl() calls, described by socketx25(7).
DEPENDENCIES
AF_CCITT
Only the SOCK_STREAM type is supported.
RETURN VALUE
If the call is successful, a valid file descriptor referencing the socket is returned. If it fails, −1 is returned and an error code is stored in errno.
DIAGNOSTICS
socket fails if:
[EHOSTDOWN] The networking subsystem has not been started up.
[EAFNOSUPPORT] The specified address family is not supported in this version of the system.
[ESOCKTNOSUPPORT] The specified socket type is not supported in this address family.
[EPROTONOSUPPORT] The specified protocol is not supported.
[EMFILE] The per-process descriptor table is full.
[ENOBUFS] No buffer space is available. The socket cannot be created.
[ENFILE] The system’s table of open files is temporarily full, and no more socket calls can be accepted.
[EPROTOTYPE] The type of socket and protocol do not match.
[ETIMEDOUT] Connection timed-out.
[EINVAL] SOCK_DGRAM sockets currently not supported for AF_UNIX address family.
AUTHOR
socket was developed by the University of California, Berkeley.
SEE ALSO
accept(2), bind(2), connect(2), getsockname(2), getsockopt(2), ioctl(2), listen(2), recv(2), select(2), send(2), shutdown(2), af_ccitt(7F), socket(7), socketx25(7), tcp(7P), udp(7P), unix(7P).
Hewlett-Packard Company — HP-UX Release 8.05: June 1991