socket
Purpose
Creates an endpoint for communication and returns a
descriptor.
Library
Sockets Library (libsock.a)
Syntax
#include <sys/types.h>
#include <sys/socket.h>
int socket (af, type, protocol)
int af, type, protocol;
Description
The socket subroutine creates an endpoint for communi-
cation and returns a socket descriptor.
The af parameter specifies an address format with which
addresses specified in later socket operations should be
interpreted. These formats are defined in the
sys/socket.h header file. The formats are:
AF_UNIX AIX path names
AF_INET ARPA Internet addresses.
The value of the type parameter specifies the semantics
of communication. AIX supports these types:
SOCK_STREAM Provides sequenced, two-way byte streams
with a transmission mechanism for out-of-
band data.
SOCK_DGRAM Provides datagrams, which are
connectionless messages of a fixed maximum
length (usually small).
The protocol parameter specifies a particular protocol to
be used with the socket. In most cases, a single pro-
tocol exists to support a particular socket type using a
given address format. When many protocols exist, you
must specify a particular protocol. Use the number for
the communication domain in which the communication takes
place.
The different types of sockets available are used for
different purposes. SOCK_DGRAM sockets allow sending
datagrams to correspondents named in send socket calls.
Programs can also receive datagrams via sockets by using
the recv subroutines.
SOCK_STREAM sockets are full-duplex byte streams. A
stream socket must be connected before any data may be
sent or received on it. Create a connection to another
socket with the connect routine. Once connected, use the
read and write system calls, or the send and recv subrou-
tines to transfer data. Issue the close system call when
a session is finished. Use the send and recv subroutines
for out-of-band data.
SOCK_STREAM communications protocols are designed to
prevent the loss or duplication of data. If a piece of
data for which the peer protocol has buffer space cannot
be successfully transmitted within a reasonable period of
time, the connection is broken. When this occurs, the
socket routines indicate an error with a return value of
-1 and with ETIMEDOUT as the specific code written to the
global variable errno. If a process sends on a broken
stream, a SIGPIPE signal is raised. Processes that
cannot handle the signal terminate.
When out-of-band data arrives on a socket, a SIGURG
signal is sent to the process group. The process group
associated with a socket may be read or set by the
SIOCGPGRP ioctl operation or the SIOCSPGRP ioctl opera-
tion. If you want to receive a signal on any data, use
both the SIOCSPGRP and FIOASYNC ioctl operations. These
ioctl operations are defined in the bsd/sys/ioctl.h file.
Sockets can be set to either blocking or non-blocking I/O
mode. The FIONBIO ioctl operation is used to determine
this mode. When FIONBIO is set, the socket is marked
non-blocking. If a read is tried and the desired data is
not available, the socket does not wait for the data to
become available, but returns immediately with the error
code EWOULDBLOCK. When FIONBIO is not set, the socket is
in blocking mode. In this mode, if a read is tried and
the desired data is not available, the calling process
waits for the data.
Similarly, when writing, if FIONBIO is set and the output
queue is full, an attempt to write causes the process to
return immediately with an error code of EWOULDBLOCK.
The operation of sockets is controlled by socket level
options. The getsockopt and setsockopt subroutines are
used to get and set these options, which are defined in
the sys/socket.h file. See "getsockopt, setsockopt" for
information on how to use these options.
Return Value
Upon successful completion, a descriptor referring to the
socket is returned. If the socket routine fails, a value
of -1 is returned, and errno is set to indicate the
error.
Diagnostics
The subroutine fails if one or more of the following are
true:
EAFNOSUPPORT The addresses in the specified address
family cannot be used with this socket.
ESOCKNOSUPPORT The socket in the specified address
family is not supported.
EMFILE The per-process descriptor table is
full.
ENOBUFS Insufficient resources were available
in the system to complete the call.
Related Information
In this book: "ioctl," "select," "accept," "bind,"
"connect," "getsockname," "getsockopt, setsockopt,"
"listen," "recv, recvfrom, recvmsg," "send, sendto,
sendmsg," "shutdown," and "socketpair."