socket() — Create a socket
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4.2
Single UNIX Specification, Version 3 |
both |
Format
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
#define _OE_SOCKETS
#include <sys/socket.h>
int socket(int *domain, int type, int protocol);
General description
- 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.
- 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.
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.
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.
- 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
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
- sys/socket.h — Sockets definitions
- accept() — Accept a new connection on a socket
- bind() — Bind a name to a socket
- close() — Close a file
- connect() — Connect a socket
- fcntl() — Control open file descriptors
- getprotobyname() — Get a protocol entry by name
- getsockname() — Get the name of a socket
- getsockopt() — Get the options associated with a socket
- ioctl() — Control device
- read() — Read from a file or socket
- readv() — Read data on a file or socket and store in a set of buffers
- recv() — Receive data on a socket
- recvfrom() — Receive messages on a socket
- recvmsg() — Receive messages on a socket and store in an array of message headers
- select(), pselect() — Monitor activity on files or sockets and message queues
- selectex() — Monitor activity on files or sockets and message queues
- send() — Send data on a socket
- sendmsg() — Send messages on a socket
- shutdown() — Shut down all or part of a duplex connection
- write() — Write data on a file or socket
- writev() — Write data on a file or socket from an array