DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

socket(SSC)


socket -- create an endpoint for communication

Syntax

cc ... -lsocket

#include <sys/types.h>
#include <sys/socket.h>

s = socket(domain, type, protocol) int s, domain, type, protocol;

Description

socket( ) creates an endpoint for communication and returns a descriptor.

s is a file descriptor returned by the socket( ) system call.

The domain parameter specifies a communications domain within which communication will take place; 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 <sys/socket.h> header file. The currently supported domains are AF_INET (Internet protocols) and AF_UNIX.

The socket has the indicated type, which specifies the semantics of communication. Currently defined types include:

SOCK_STREAM
SOCK_DGRAM
SOCK_RAW

The AF_INET domain supports all of the above listed types. The AF_UNIX domain supports only the SOCK_STREAM and SOCK_DGRAM types.

Note that not all types are supported by all protocol families.

A SOCK_STREAM type provides sequenced, reliable, two-way connection-based byte streams with an out-of-band data transmission mechanism. A SOCK_DGRAM socket supports datagrams (connectionless, unreliable messages of a fixed maximum length).

SOCK_RAW sockets provide access to internal network protocols and interfaces. This type is available only to the super-user.

The protocol parameter specifies a particular 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, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner. The protocol number to use is particular to the ``communication domain'' in which communication is to take place; see protocols(SFF).

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(SSC) call. (If the connect( ) call is unsuccessful, a second connect( ) call to the same socket is not allowed. The socket must be closed with a close(S) system call and a new socket created using this socket( ) routine.) Once connected, data may be transferred using read(S) and write(S) calls or some variant of the send(SSC) and recv(SSC) calls. When a session has been completed, a close(S) may be performed. If the connection fails at any time during the session (because of a network failure, for example), the socket cannot be re-used. In other words, a new connect( ) call cannot be invoked to re-establish the connection with the remote socket). The current socket must be closed and a new socket created. Out-of-band data may also be transmitted over a SOCK_STREAM as described in send(SSC) and received as described in recv(SSC).

The communications protocols used to implement a SOCK_STREAM insure 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 and calls will indicate an error with -1 returns and with ETIMEDOUT as the specific code in the global variable errno. Some protocols optionally keep sockets ``warm'' 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 a extended period (for example, 5 minutes). A SIGPIPE signal is raised if a process sends on a broken stream; this causes naive processes, which do not handle the signal, to exit.

SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to correspondents named in send(SSC) calls. Datagrams are generally received with recv(SSC), which returns the next datagram with its return address.

An ioctl(S) call can be used to specify that a process group receive a SIGURG (SIGUSR1) signal when the out-of-band data arrives. It may also enable non-blocking I/O and asynchronous notification of I/O events with SIGIO (SIGPOLL) signals.

The operation of sockets is controlled by socket level options. These options are defined in the <sys/socket.h> header file. setsockopt( ) and getsockopt( ) are used to set and get options, respectively. See getsockopt(SSC) for more information.

Return values

A -1 is returned if an error occurs, otherwise the return value is a descriptor referencing the socket.

Diagnostics

The socket( ) call fails if:

[EPROTONOSUPPORT]
The protocol type or the specified protocol is not supported within this communication domain.

[EMFILE]
The per-process descriptor table is full.

[ENFILE]
The system file table is full.

[EACCES]
Permission to create a socket of the specified type and/or protocol is denied.

[ENOSR]
Insufficient buffer space is available. The socket cannot be created until sufficient resources are freed.

[EPROTOTYPE]
The protocol is the wrong type for the socket.

See also

Intro(ADMP), Intro(SSC), accept(SSC), bind(SSC), connect(SSC), getsockname(SSC), getsockopt(SSC), inet(ADMP), ioctl(S), listen(SSC), protocols(SFF), read(S), recv(SSC), select(S), send(SSC), shutdown(SSC), unix(ADMP), write(S)
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003