Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ socket(3N) — GL2 W2.3

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

accept(3N)

connect(3N)

receive(3N)

send(3N)

socketaddr(3N)

inet(7)

SOCKET(3N)  —  Silicon Graphics

NAME

socket − create an endpoint for communication

SYNOPSIS

#include <sys/socket.h> s = socket(type, pf, addr, options);
int s, type;
struct sockproto *pf;
struct sockaddr *addr;
int options;

DESCRIPTION

Socket creates a communication endpoint and returns a descriptor, much like a file descriptor.  The socket has a type which defines the semantics for communication.  Currently defined types are: SOCK_STREAM, for sequenced, reliable, two-way connection based streams with an out-of-band mechanism; SOCK_DGRAM, for datagrams, connectionless, unreliable messages of a fixed (typically small) maximum length; SOCK_RAW, for access to internal network interfaces.  SOCK_RAW is only accessible by the super-user and is not described here. 

pf causes a specific protocol to be used with the socket.  (Currently only one protocol supports each socket type.) 

The addr parameter specifies the socket address.  A socket address is a discriminated union 16 bytes long.  The first two bytes indicate the format of remaining bytes. 

The only variant to this rule is an Internet address (sockaddr_in).  The first field of this variable type is AF_INET (this address is part of the Internet Address Family) which is defined in <sys/socket.h>. 

The second and third fields are a 16-bit socket number (see <net/in.h> for lists of well-known sockets) and a 32-bit Internet host address.  The socket number and host address are in network byte order. 

If no socket address is specified, the system assigns one.  It does this at connection time to simplify the routing decisions required of the connected socket.  If the socket number is omitted, a unique socket number is supplied.  The socket numbers 0 through IPPORT_RESERVED-1 are reserved for the super-user. 

The procedure rhost(3x) may be used to convert host names to Internet addresses, and raddr(3x) to convert addresses to standard host names.

Sockets of type SOCK_STREAM are full-duplex byte streams, similar to two-way pipes.  Typically a stream is created via socket and connected to another socket via a call to connect or accept.  Then a sequence of read and write calls exchanges data.  Finally, close(2) terminates the connection.

A typical sequence is

CLIENT         SERVER socket()       socket()
connect()      accept()
write()        read()
read()         write()
   .             .
   .             .
close()

Out-of-band data may also be transmitted as described below. 

Sockets of the type 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 (1 minute), then the connection is considered broken and calls indicate an error with −1 returns.  ETIMEDOUT is the specific code in the global variable errno. 

The protocols optionally keep sockets “warm” by forcing transmissions roughly every minute in the absence of other activity.  An error is flagged if no response can be elicited on an otherwise idle connection for an extended period (5 minutes).  A SIGPIPE signal is raised if a process writes on a broken stream.  This causes naive processes, which do not handle the signal, to exit. 

SOCK_DGRAM sockets send datagrams to correspondents named in send(3N) calls.  It is also possible to receive datagrams at such a socket with receive(3N). 

A typical sequence is

SENDER        RECEIVER s=socket()    s=socket()
    .             .
    .             .
send(s)       receive(f)
    .             .
    .             .
receive(f)    send(s)

connect may be used with datagram sockets to fix the receipient/sender for furture communication.  Once a datagram has been connected, read may be used in place of send, and write may be used in place of receive. 

The subroutine socketaddr(3N) can be used to determine the address of a socket. 

The socket options are OR-ed together in options:

SO_ACCEPTCONN
must be used with SOCK_STREAM sockets which accept connections.  Only sockets which indicate SO_ACCEPTCONN as a creation parameter may accept(3N), and such sockets may not connect(3N). 

SO_DONTLINGER
allows close(2) operations on a socket to finish immediately.  Otherwise the system will block a process waiting for data to drain (or return EWOULDBLOCK if the socket is marked NONBLOCKING) when a close is attempted.  See the SIOCSLINGER ioctl below also. 

SO_KEEPALIVE
causes "keep alive" to be used to time-out dead connections. If this option is not specified then timing out dead connections is the responsibility of the user process.

General ioctl’s which apply to sockets are:

SIOCDONE
indicates the user has finished receiving (if the integer parameter is 0), sending (if the integer parameter is 1) or both (if the parameter is 2) on the indicated socket. This indicates an end-of-file in a SOCK_STREAM if reading input.

SIOCSLINGER
sets the linger time in seconds. This is currently only partially implemented: linger time is either 0 or infinite (if non-zero).

SIOCGLINGER
returns the current linger time.

SEE ALSO

accept(3N), connect(3N), receive(3N), send(3N), socketaddr(3N), inet(7)

Version 2.3  —  July 04, 1985

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026