Description |
|
The socket call creates an endpoint for communication
and returns a descriptor. The socket descriptor returned is used in all subsequent socket-related system calls.he 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 file
<sys/socket.h>. The only currently supported address families
are as follows:
AF_INET (DARPA Internet addresses)
AF_UNIX (directory path names on a local node)
|
The type parameter specifies the communication semantics for the socket. Currently defined types are as follows:
A SOCK_STREAM
type provides sequenced, reliable, two-way, connection-based byte
streams. A SOCK_DGRAM socket supports
datagrams, which are connection-less, unreliable messages of a fixed, typically small, maximum length.
The protocol parameter specifies a particular protocol
to be used with the socket. The protocol number to use depends on
the communication domain in which communication is to take place. (Refer
to the chapter on name services routines.) Protocol can be
supplied as zero, in which case the system chooses a protocol type to use, based on the socket type.
Sockets of type SOCK_STREAM are byte streams similar to UNIX 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 send and recv
calls or read and write calls. When a session
has been completed, a close can be performed.
The communications
protocol (TCP) 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 call indicates an error
with errno set to ETIMEDOUT. An end-of-file condition
(zero bytes read) is returned if a process tries to read on a broken
stream. To use the errno global variable, include the file
<sys/errno.h>.
SOCK_DGRAM sockets
allow sending of messages to correspondents named in sendto
calls. It is also possible to receive messages at a SOCK_DGRAM
socket with recvfrom. The
sockets operation is controlled by socket level options set by the setsockopt system call. (Refer to getsockopt or setsockopt.) These options are defined in the file <sys/socket.h>.
Return Value |
|
If the call is successful, a valid file descriptor referencing the
socket is returned. If it fails, a -1 is returned, and an error code
is stored in errno.