1SOCKET(7) Linux Programmer's Manual SOCKET(7)
2
3
4
6 socket - Linux socket interface
7
9 #include <sys/socket.h>
10
11 sockfd = socket(int socket_family, int socket_type, int protocol);
12
14 This manual page describes the Linux networking socket layer user in‐
15 terface. The BSD compatible sockets are the uniform interface between
16 the user process and the network protocol stacks in the kernel. The
17 protocol modules are grouped into protocol families such as AF_INET,
18 AF_IPX, and AF_PACKET, and socket types such as SOCK_STREAM or
19 SOCK_DGRAM. See socket(2) for more information on families and types.
20
21 Socket-layer functions
22 These functions are used by the user process to send or receive packets
23 and to do other socket operations. For more information see their re‐
24 spective manual pages.
25
26 socket(2) creates a socket, connect(2) connects a socket to a remote
27 socket address, the bind(2) function binds a socket to a local socket
28 address, listen(2) tells the socket that new connections shall be ac‐
29 cepted, and accept(2) is used to get a new socket with a new incoming
30 connection. socketpair(2) returns two connected anonymous sockets (im‐
31 plemented only for a few local families like AF_UNIX)
32
33 send(2), sendto(2), and sendmsg(2) send data over a socket, and
34 recv(2), recvfrom(2), recvmsg(2) receive data from a socket. poll(2)
35 and select(2) wait for arriving data or a readiness to send data. In
36 addition, the standard I/O operations like write(2), writev(2), send‐
37 file(2), read(2), and readv(2) can be used to read and write data.
38
39 getsockname(2) returns the local socket address and getpeername(2) re‐
40 turns the remote socket address. getsockopt(2) and setsockopt(2) are
41 used to set or get socket layer or protocol options. ioctl(2) can be
42 used to set or read some other options.
43
44 close(2) is used to close a socket. shutdown(2) closes parts of a
45 full-duplex socket connection.
46
47 Seeking, or calling pread(2) or pwrite(2) with a nonzero position is
48 not supported on sockets.
49
50 It is possible to do nonblocking I/O on sockets by setting the O_NON‐
51 BLOCK flag on a socket file descriptor using fcntl(2). Then all opera‐
52 tions that would block will (usually) return with EAGAIN (operation
53 should be retried later); connect(2) will return EINPROGRESS error.
54 The user can then wait for various events via poll(2) or select(2).
55
56 ┌