socket(2) CLIX socket(2)
NAME
socket - Creates an endpoint for communication
LIBRARY
Berkeley Software Distribution Library (libbsd.a)
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int socket(
int domain ,
int type ,
int protocol );
PARAMETERS
domain Represents a communications domain.
type Specifies the communication semantics.
protocol Represents the protocol for the socket.
DESCRIPTION
The socket() function creates an endpoint for communication and returns a
descriptor.
The domain parameter specifies a communications domain within which
communication will occur; this selects the protocol family that should be
used. The protocol family generally is the same as the address family for
the addresses supplied in later operations on the socket. These families
are defined in the include file <sys/socket.h>. The currently understood
formats are as follows:
AF_UNIX (UNIX internal protocols)
AF_INET (ARPA Internet protocols)
The socket has the indicated type, which specifies the semantics of
communication. Currently defined types are as follows:
⊕ SOCK_STREAM
⊕ SOCK_DGRAM
A SOCK_STREAM type provides sequenced, reliable, two-way connection-based
2/94 - Intergraph Corporation 1
socket(2) CLIX socket(2)
byte streams. An out-of-band data transmission mechanism may be
supported. A SOCK_DGRAM socket supports datagrams (connectionless,
unreliable messages of a fixed (typically small) maximum length).
The protocol parameter specifies a protocol to be used with the socket.
Normally, only a single protocol exists to support a particular socket
type within a given protocol family. However, many protocols may exist.
In this case, a protocol must be specified in this manner. The protocol
number to use is particular to the communication domain in which
communication is to occur. (See the protocols file.)
Sockets of type SOCK_STREAM are full-duplex byte streams similar to pipes.
A stream socket must be in a connected state before any data may be sent
or received on it. A connection to another socket is created with a
connect() call. Once connected, data may be transferred using read() and
write() calls, the readv() and writev() calls, or some variant of the
send() and recv() calls. When a session has been completed, a close()
call may be performed. Out-of-band data may also be transmitted as
described in send() and received as described in recv().
The communications protocols used to implement a SOCK_STREAM ensure that
data is not lost or duplicated. If a piece of data for which the peer
protocol has buffer space cannot be successfully transmitted within a
reasonable length of time, the connection is considered broken. Also,
calls will indicate an error with -1 returned and with ETIMEDOUT as the
specific code in the global variable errno. The protocols optionally keep
sockets open by forcing transmissions roughly every minute in the absence
of other activity. An error is then indicated if no response can be
elicited on an otherwise idle connection for an extended period (such as
five 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.
SOCK_DGRAM sockets allow datagrams to be sent to correspondents named in
send() calls. Datagrams are generally received with recvfrom(), which
returns the next datagram with its return address.
The fcntl() function can be used to specify a process group to receive a
SIGURG signal (see signal(2)) when the out-of-band data arrives. It may
also enable nonblocking I/O and asynchronous notification of I/O events
with the SIGIO signal.
The operation of sockets is controlled by socket-level options. These
options are defined in the file <sys/socket.h>. The setsockopt() and
getsockopt() functions are used to set and get options, respectively.
EXAMPLES
To create a connection oriented socket in the Internet domain with a
default protocol:
if (socket(AF_INET, SOCK_STREAM, 0) == -1)
2 Intergraph Corporation - 2/94
socket(2) CLIX socket(2)
perror("Socket failed");
NOTES
Datagram sockets are not supported in the UNIX domain.
RETURN VALUES
Upon successful completion, a descriptor referencing the socket is
returned. Otherwise, a value of -1 is returned and errno is set to
indicate the error.
ERRORS
The socket() function fails if one or more of the following is true:
[EMFILE]
The per-process descriptor table is full.
[ENFILE]
The system file table is full.
[ENOBUFS]
The resources to support the connection are not available.
[EPROTONOSUPPORT]
The specified domain, type or protocol is not supported.
[ESOCKTNOSUPPORT]
The socket type is not supported for the specified domain.
[ENXIO]
The system cannot access the specified device.
[EIO] The protocol was not initialized.
[EINTR]
A signal was caught during the socket() function.
RELATED INFORMATION
Functions: read(2), write(2), accept(2), bind(2), connect(2),
getsockname(2), getsockopt(2), listen(2), readv(2), recv(2), select(2),
send(2), shutdown(2), socketpair(2), writev(2), signal(2)
Files: intro(7)
CLIX Programming Guide
2/94 - Intergraph Corporation 3