Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

socket() - create socket

&pagelevel(3)&pagelevel

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


Kernighan-Ritchie-C:

int socket(domain, type, protocol);

int domain, type, protocol;

ANSI-C:
int socket(int domain, int type, int protocol);


Description

The socket() function creates a communications endpoint and returns a descriptor.

The domain parameter defines the communications domain in which communications are to take place. This also defines the protocol family to be used and thus the family of the addresses used for later operations on the socket. These families are defined in the <sys/socket.h> header file. The AF_INET, AF_INET6 and AF_ISO address families are supported.

The type parameter defines the type of the socket and the semantics of the communications. The following socket types are currently defined:

  • SOCK_STREAM

  • SOCK_DGRAM

  • SOCK_RAW

Each of these types is supported in the AF_INET and AF_INET6 address families. Only the type SOCK_STREAM is defined in the AF_ISO address family.

The protocol parameter is not evaluated.

Socket operations are controlled by socket level options and defined in the <sys.socket.h> header file. The user can get and set these options with the getsockopt() and setsockopt() functions, respectively.


AF_INET and AF_INET 6 address families

A socket of type SOCK_STREAM provides a secured and bidirectional connection on which data is transmitted sequentially. A stream socket must be connected to another stream socket before any data can be sent or received on it.

A connection to another socket is set up when one socket requests a connection to a partner socket with connect(), and the partner system confirms the connection with accept(). After the connection has been successfully established, both partners can transmit data with soc_read() or soc_readv() and soc_write() or soc_writev() or similar calls such as send() and recv().

A socket of type SOCK_DGRAM supports the transmission of datagrams. A datagram is a connectionless, unsecured message with a fixed maximum length. The sendto() function sends datagrams from one datagram socket to another datagram socket specified in the sendto() call. Conversely, datagrams are received with the recvfrom() function. recvfrom() returns the next datagram together with the address of the sender.

If the communications partner for a datagram socket has been preset with the function connect(), the functions send() and recv() can also be used for this datagram socket.

A socket of the type SOCK_RAW supports the transmission of ICMP/ICMPv6 messages.


AF_ISO address family

A socket of type SOCK_STREAM provides a secured and bidirectional connection on which a record-oriented and sequential transfer of data takes place. A stream socket must be connected to another stream socket before any data can be sent or received on it.

A connection to another socket is set up when one socket requests a connection to a partner socket with connect(), and the partner system confirms the connection with accept() and one of the functions send(), soc_write() or sendmsg().

After the connection has been successfully established, both partners can transmit data with soc_read() or soc_readv() and soc_write() or soc_writev() or similar calls such as send() and recv() or sendmsg() and recvmsg().

Return value

>=0:

Designates a non-negative descriptor if successful.

-1:

If errors occur. errno is set to indicate the error.

Errors indicated by errno

EMFILE

The table of descriptors per task is full; the maximum number of socket descriptors that can be processed concurrently has been reached. This maximum value can be determined with the getdtablesize() function.

ENOBUFS

There is not enough storage space in the buffer. The socket cannot be created until sufficient storage resources have been freed.

EAFNOSUPPORT

The address family is not supported by this protocol family. The specified address is incompatible with the protocol used.

ENOMEM

Memory bottleneck. Not enough virtual storage space could be assigned when executing the function.

EPROTONOSUPPORT

The socket type is not supported in this domain.

See also

accept(), bind(), connect(), getsockname(), getsockopt(), listen(), recv(), recvfrom(), select(), send(), sendto(), soc_close(), soc_ioctl(), soc_read(), soc_readv(), soc_write(), soc_writev()