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 socketpair(int *domain, int type, int protocol, int socket_vector[2]);
Berkeley
sockets: #define _OE_SOCKETS
#include <sys/socket.h>
int socketpair(int *domain, int type, int protocol, int sv[2]);
General description
The socketpair()
function acquires a pair of sockets of the type specified that are
unnamed and connected in the specified domain and using the specified
protocol. For socket pairs in the AF_UNIX domain, the protocol
must be
0.
- Parameter
- Description
- domain
- The domain in which to open the socket. Although socket pairs
can be obtained for AF_INET domain sockets, it is recommended that
AF_UNIX domain sockets be used for socket pairs.
- type
- The type of socket created, either SOCK_STREAM, or SOCK_DGRAM.
- protocol
- The protocol requested must be 0.
- sv
- The descriptors used to refer to the obtained sockets.
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, socketpair()
returns a nonnegative socket descriptor.
If unsuccessful, socketpair()
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.
- EFAULT
- sv is not in the writable part of the
user's address space.
- EINVAL
- The request is invalid or not supported.
- EMFILE
- Too many files are open for this process.
- ENFILE
- Too many files are open in the system.
- ENOBUFS
- Insufficient system resources are available to complete the call.
- EOPNOSUPPORT
- The protocol does not allow for the creation of socket pairs.
- 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 socketpair() call.
#include <types.h>
#include <sys/socket.h>
int sv[2];
⋮
/* Get stream socket in UNIX
domain with default protocol */
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0)
printf ("Error occurred while trying to get a socket pair.\n");
else
⋮