socket(2)
socket
- create an endpoint for communication
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
domain
(protocol family)
The domain
argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>
. The formats currently understood by the Linux kernel include:
AF_UNIX |
Local communication | unix(7) |
AF_INET |
IPv4 Internet protocols | ip(7) |
AF_INET6 |
IPv6 Internet protocols | ipv6(7) |
Further details of the above address families, as well as information on several other address families, can be found in address_families(7).
NOTE:
AF***
中的AF
是**address family**的缩写。思考: **address family**的含义是什么?
type
The socket has the indicated type, which specifies the communication semantics. Currently defined types are:
SOCK_STREAM
TCP
SOCK_DGRAM
UDP
SOCK_SEQPACKET
SOCK_RAW
protocol
The protocol
specifies a particular protocol to be used with the socket.
Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0.
NOTE:
一一对应关系
However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner. The protocol number to use is specific to the “communication domain” in which communication is to take place; see protocols(5). See getprotoent(3) on how to map protocol name strings to protocol numbers.
Sockets of type SOCK_STREAM
Out-of-band data
Out-of-band data may also be transmitted as described in send(2) and received as described in recv(2).
SO_KEEPALIVE
When SO_KEEPALIVE
is enabled on the socket the protocol checks in a protocol-specific manner if the other end is still alive.
SIGPIPE
signal
A SIGPIPE
signal is raised if a process sends or receives on a broken stream; this causes naive processes, which do not handle the signal, to exit.
NOTE:
一般,对于这个signal,都是 ,比如
SIG_IGN
tag-Redis Signal Handle-signal disposition-
signal(SIGPIPE, SIG_IGN)
Sockets of type SOCK_SEQPACKET
SOCK_SEQPACKET
sockets employ the same system calls as SOCK_STREAM
sockets. The only difference is that read(2) calls will return only the amount of data requested, and any data remaining in the arriving packet will be discarded. Also all message boundaries in incoming datagrams are preserved.
Sockets of type SOCK_DGRAM
and SOCK_RAW
Socket signals
An fcntl(2)
F_SETOWN
operation can be used to specify a process or process group to receive a SIGURG
signal when the out-of-band data arrives or SIGPIPE
signal when a SOCK_STREAM
connection breaks unexpectedly. This operation may also be used to set the process or process group that receives the I/O and asynchronous notification of I/O events via SIGIO
. Using F_SETOWN
is equivalent to an ioctl(2) call with the FIOSETOWN
or SIOCSPGRP
argument.
Operation of sockets
The operation of sockets is controlled by socket level options. These options are defined in setsockopt(2)
and getsockopt(2)
are used to set and get options.