socket() — Create a socket

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

X/Open:
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>

int socket(int domain, int type, int protocol);
Berkeley sockets:
#define _OE_SOCKETS
#include <sys/socket.h>

int socket(int *domain, int type, int protocol);

General description

The socket() function creates an endpoint for communication and returns a socket descriptor representing the endpoint. Different types of sockets provide different communication services.
Parameter
Description
domain
The address domain requested, either AF_INET, AF_INET6, AF_UNIX, or AF_RAW.
type
The type of socket created, either SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW.
protocol
The protocol requested. Some possible values are 0, IPPROTO_UDP, or IPPROTO_TCP.

The domain parameter specifies a communication domain within which communication is to take place. This parameter selects the address family (format of addresses within a domain) that is used. The families supported are AF_INET and AF_INET6, which is the Internet domain, and AF_UNIX, which is the local socket domain. These constants are defined in the sys/socket.h include file.

The type parameter specifies the type of socket created. The type is analogous with the semantics of the communication requested. These socket type constants are defined in the sys/socket.h include file. The types supported are:
Socket Type
Description
SOCK_DGRAM
Provides datagrams, which are connectionless messages of a fixed maximum length whose reliability is not guaranteed. Datagrams can be corrupted, received out of order, lost, or delivered multiple times. This type is supported in the AF_INET, AF_INET6, and AF_UNIX domains.
SOCK_RAW
Provides the interface to internal protocols (such as IP and ICMP). This type is supported in the AF_INET and AF_INET6 domains. You must be a superuser to use this type.
SOCK_STREAM
Provides sequenced, two-way byte streams that are reliable and connection-oriented. They support a mechanism for out-of-band data. This type is supported in the AF_INET, AF_INET6, and AF_UNIX domains.
Understanding the socket() Parameters: The protocol parameter specifies a particular protocol to be used with the socket. In most cases, a single protocol exists to support a particular type of socket in a particular address family. If the protocol parameter is set to 0, the system selects the default protocol number for the domain and socket type requested. Protocol numbers are found in the tcpip.ETC.PROTO data set. Alternatively, the getprotobyname() call can be used to get the protocol number for a protocol with a known name.
Note: The protocol field must be set to 0, if the domain parameter is set to AF_UNIX.

SOCK_STREAM sockets model duplex-byte streams. They provide reliable, flow-controlled connections between peer application programs. Stream sockets are either active or passive. Active sockets are used by clients who start connection requests with connect(). By default, socket() creates active sockets. Passive sockets are used by servers to accept connection requests with the connect() call. You can transform an active socket into a passive socket by binding a name to the socket with the bind() call and by indicating a willingness to accept connections with the listen() call. After a socket is passive, it cannot be used to start connection requests.

In the AF_INET and AF_INET6 domains, the bind() call applied to a stream socket lets the application program specify the networks from which it is willing to accept connection requests. The application program can fully specify the network interface by setting the Internet address field in the address structure to the Internet address of a network interface. Alternatively, the application program can use a wildcard to specify that it wants to receive connection requests from any network. For AF_INET sockets, this is done by setting the Internet address field in the address structure to the constant INADDR_ANY, as defined in <netinet/in.h>. For AF_INET6 sockets, this is done by setting the Internet address field in the address structure to in6addr_any as defined in <netinet/in.h>.

After a connection has been established between stream sockets, any of the data transfer calls can be used: (read(), readv(), recv(), recvfrom(), recvmsg(), send(), sendmsg(), sendto(), write(), and writev()). Usually, the read()-write() or send()-recv() pairs are used for sending data on stream sockets. If out-of-band data is to be exchanged, the send()-recv() pair is normally used.

SOCK_DGRAM sockets model datagrams. They provide connectionless message exchange without guarantees of reliability. Messages sent have a maximum size. Datagram sockets are supported in the AF_UNIX domain.

There is no active or passive analogy to stream sockets with datagram sockets. Servers must still call bind() to name a socket and to specify from which network interfaces it wishes to receive packets. Wildcard addressing, as described for stream sockets, applies for datagram sockets also. Because datagram sockets are connectionless, the listen() call has no meaning for them and must not be used with them.

After an application program has received a datagram socket, it can exchange datagrams using the sendto() and recvfrom(), or sendmsg() and recvmsg() calls. If the application program goes one step further by calling connect() and fully specifying the name of the peer with which all messages will be exchanged, then the other data transfer calls read(), write(), readv(), writev(), send(), and recv() can also be used. For more information on placing a socket into the connected state, see connect() — Connect a socket.

Datagram sockets allow messages to be broadcast to multiple recipients. Setting the destination address to be a broadcast address is network-interface-dependent (it depends on the class of address and whether subnets—logical networks divided into smaller physical networks to simplify routing—are used). The constant INADDR_BROADCAST, defined in netinet/in.h, can be used to broadcast to the primary network if the primary network configured supports broadcast.

Outgoing packets have an IP header prefixed to them. IP options can be set and inspected using the setsockopt() and getsockopt() calls, respectively. Incoming packets are received with the IP header and options intact.

Sockets are deallocated with the close() call.
Note: For AF_UNIX, when closing sockets that were bound, you should also use unlink() to delete the file created at bind() time.

Special behavior for C++: To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.

Returned value

If successful, socket() returns a nonnegative socket descriptor.

If unsuccessful, socket() returns -1 and sets errno to one of the following values:
Error Code
Description
EACCES
Permission to create a socket of the specified type or protocol is denied.
EAFNOSUPPORT
The address family is not supported (it is not AF_UNIX, AF_INET, or AF_INET6).
EAGAIN
Resource temporarily unavailable.
EINVAL
The request is invalid or not supported.
EIO
There has been a network or transport failure.
ENOBUFS
Insufficient system resources are available to complete the call.
ENOENT
There was no NETWORK statement in the parmlib member to match the specified domain.
EPROTONOSUPPORT
The protocol is not supported in this domain or this protocol is not supported for this socket type.
EPROTOTYPE
The socket type is not supported by the protocol.

Example

The following are examples of the socket() call.
int s;
char *name;
int socket(int domain, int type, int protocol);
⋮
/* Get stream socket in Internet 
domain with default protocol */
s = socket(AF_INET, SOCK_STREAM, 0);
⋮
/* Get stream socket in local socket 
domain with default protocol */
s = socket(AF_UNIX, SOCK_STREAM, 0);

Related information