Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

accept(2)

bind(2)

connect(2)

getsockname(2)

getsockopt(2)

ioctl(2)

listen(2)

recv(2)

select(2)

send(2)

shutdown(2)

socket(7)

tcp(7P)

udp(7P)

SOCKET(2)  —  Series 300 and 800 Only

NAME

socket − create an endpoint for communication

SYNOPSIS

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

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

DESCRIPTION

Socket creates an endpoint for communication and returns a descriptor.  The socket descriptor returned is used in all subsequent socket-related system calls. 

The 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 family is:

AF_INET (DARPA Internet addresses)

The type specifies the semantics of communication for the socket.  Currently defined types are:

SOCK_STREAM
SOCK_DGRAM

A SOCK_STREAM type provides sequenced, reliable, two-way, connection based byte streams.  A SOCK_DGRAM socket supports datagrams (connectionless, unreliable messages of a fixed, typically small, maximum length). 

The protocol specifies a particular protocol to be used with the socket.  Normally, only a single protocol exists to support a particular socket type using a given address family.  However, many protocols may exist, in which case a particular protocol must be specified. The protocol number to use depends on the “communication domain” in which communication is to take place. (See services(4) and protocols(4).) Protocol can be supplied as zero, in which case the system chooses a protocol type to use. 

Sockets of type SOCK_STREAM are byte streams similar to pipes except 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(2) or accept(2) call.  Once connected, data can be transferred using some variant of the send(2) and recv(2) or the read(2) and write(2) calls.  When a session has been completed, a close(2) can be performed.

TCP, the communications protocol used to implement SOCK_STREAM, insures that data are 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(2) call indicates an error with errno set to ETIMEDOUT.  If SO_KEEPALIVE is set, the TCP protocol keeps otherwise inactive socket connections active by forcing transmissions every 45 seconds.  These transmissions are not visible to users, and cannot be read by a recv(2) call. An error is indicated if no response can be elicited on an otherwise idle connection for a extended period (e.g., 6 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. An end-of-file condition (zero bytes read) is returned if a process tries to read on a broken stream.

SOCK_DGRAM sockets allow sending of messages to correspondents named in send(2) calls. It is also possible to receive messages at such a socket with recv(2).

The operation of sockets is controlled by socket level options set by the setsockopt system call described in the getsockopt(2) reference page.  These options are defined in the file <sys/socket.h> and explained in the getsockopt(2) reference page. 

RETURN VALUE

If the call is successful, a valid file descriptor referencing the socket is returned. If it fails, −1 is returned and an error code is stored in errno. 

DIAGNOSTICS

The socket call fails if:

[EHOSTDOWN] The networking subsystem has not been started up. 

[EAFNOSUPPORT] The specified address family is not supported in this version of the system. 

[ESOCKTNOSUPPORT] The specified socket type is not supported in this address family. 

[EPROTONOSUPPORT] The specified protocol is not supported. 

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

[ENOBUFS] No buffer space is available.  The socket cannot be created. 

[ENFILE] The system’s table of open files is temporarily full, and no more socket calls can be accepted. 

[EPROTOTYPE] The type of socket and protocol do not match. 

[ETIMEDOUT] Connection timed-out. 

DEPENDENCIES

Implemented on the Series 300 and 800 only. 

AUTHOR

UCB (University of California at Berkeley)

SEE ALSO

accept(2), bind(2), connect(2), getsockname(2), getsockopt(2), ioctl(2), listen(2), recv(2), select(2), send(2), shutdown(2), socket(7), tcp(7P), udp(7P). 

Hewlett-Packard Company  —  May 11, 2021

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