socket 中文man页面

系统
本手册页介绍了 Linux 套接字的用户接口. 这个 BSD 兼容套接字是介于用户进程与内核网络协议栈之间的统一接口, 各协议模块属于不同的 协议族 ,如 PF_INET, PF_IPX, PF_PACKET 和 套接字类型 ,如 字节流(SOCK_STREAM) 或 数据报(SOCK_DGRAM). 关于协议族和套接字类型请参考 socket(2).

NAME

socket - Linux 套接字  

总览

#include <sys/socket.h>
mysocket = socket(int socket_family, int socket_type, int protocol);

描述

本手册页介绍了 Linux 套接字的用户接口. 这个 BSD 兼容套接字是介于用户进程与内核网络协议栈之间的统一接口, 各协议模块属于不同的 协议族 ,如 PF_INET, PF_IPX, PF_PACKET套接字类型 ,如 字节流(SOCK_STREAM)数据报(SOCK_DGRAM). 关于协议族和套接字类型请参考 socket(2).

套接层函数

用户通过这些套接字函数发送和接收包, 以及其他套接字操作. 详细说明参看他们各自的手册页.

socket(2) 创建套接字,

connect(2) 与远程套接字地址建立连接

bind(2) 把套接字和一个本地套接字地址绑定在一起(为套接字分配一个本地协议地址)

listen(2) 通知套接字接受新的连接

accept(2) 为新的已完成连接获得新的描述字

socketpair(2) 返回两个连接的匿名套接字(仅在某些本地族中才有实现,如 PF_UNIX

send(2),

sendto(2), 和 sendmsg(2) 通过套接字发送数据,而 recv(2), recvfrom(2), recvmsg(2) 从套接字接收数据. poll(2) 和

select(2) 等待数据到来或准备好接收数据. 除此之外, 标准 I/O 操作如 write(2), writev(2), sendfile(2), read(2), 和 readv(2) 也可用来读入(接收)和写出(发送)数据.

getsockname(2) 用于获得本地套接字地址

getpeername(2) 用于获得远端套接字地址. getsockopt(2) 和 setsockopt(2) 用于设置或取得套接字或协议选项. ioctl(2) 也可以用来设置或读取一些其他选项.

close(2) 关闭套接字. shutdown(2) 关闭全双工套接字连接的一部分.

套接字不支持搜索,也不支持调用 pread(2) 或 pwrite(2) 进行非 0 位置的操作. 可以用 fcntl(2). 设置 O_NONBLOCK 标志来实现对套接字的非阻塞 I/O 操作 O_NONBLOCK 是从 accept 继承来的,然后原来所有会阻塞的操作会返回 EAGAIN. connect(2) 在此情况下返回 EINPROGRESS 错误. 用户可以通过 poll(2) 或者 select(2) 等待各种事件.

I/O 事件
 
事件 轮询标志 发生事件
 
POLLIN 新数据到达.
 
POLLIN (对面向连接的套接字)建立连接成功
 
POLLHUP 另一端套接字发出断开连接请求.
 
POLLHUP (仅对面向连接协议)套接字写的时候连接断开. 同时发送 SIGPIPE.
 
POLLOUT 套接字有充足的发送缓冲区用于写入新数据.
 
读/写 POLLIN|
POLLOUT
发出的 connect(2) 结束.
 
读/写 POLLERR 产生一个异步错误.
 
读/写 POLLHUP 对方已经单向关闭连接.
 
例外 POLLPRI 紧急数据到达.然后发送 SIGURG.
 

另外一个的 poll/select 方法是让内核用 SIGIO 信号来通知应用程序. 要这么用的话你必须用 fcntl(2) 设置套接字文件描述符的 FASYNC 标志,并用 sigaction(2). 给 SIGIO 信号设置一个的有效信号处理句柄.参看下面的 SIGNALS 的讨论.  

套接字选项

套接字选项可以用 setsockopt(2) 来设置,用 getsockopt(2) 读取所有套接字级别设为 SOL_SOCKET 的套接字的套接字选项:

SO_KEEPALIVE
允许在面向连接的套接字上发送 keep-alive 消息的功能.是一个布尔整数.
SO_OOBINLINE
如果打开这个选项,带外(Out-of-Band)数据可以直接放入接收数据流。否则,只有接收时打开 MSG_OOB 标志, 才接收带外数据.
SO_RCVLOWATSO_SNDLOWAT
声明在开始向协议 (SO_SNDLOWAT) 或正在接收数据的用户 (SO_RCVLOWAT). 传递数据之前缓冲区内的最小字节数. 在 Linux 中这两个值是不可改变的, 固定为 1 字节. 可以用 getsockopt 用来读取它们的值; setsockopt 总是返回 ENOPROTOOPT.
SO_RCVTIMEOSO_SNDTIMEO
发送和接收时的超时设定, 并在超时时报错. 在 Linux 中由协议指定, 不能被读写. 它们的功能可用 alarm(2) 或者 setitimer(2). 来模拟.
SO_BSDCOMPAT
允许 BSD 的 bug-to-bug 兼容. 这一项只能在 UDP 协议模块中使用而且今后将要取消. 如果允许的话, UDP 套接字接收到的 ICMP 错误将不会被传送至用户程序. Linux 2.0 中对于原始套接字也允许 BSD bug-to-bug 兼容(报头随机改变,省略广播标识),但在 Linux 2.2 中取消了这一项. 修改用户程序的方式比较好.
SO_PASSCRED
允许或关闭 SCM_CREDENTIALS 控制消息的接收. 更多信息参见 unix(7).
SO_PEERCRED
返回连接至此套接字的外部进程的身份验证. 只在 PF_UNIX 套接字中有用.参见 unix(7). 参数为 ucred 结构.只在 getsockopt. 中有效.
SO_BINDTODEVICE
将此套接字绑定到一个特定的设备上, 如lqeth0rq, 做为指定的接口名字传递. 如果名称是空字符串或此项长度为 0, 则套接字设备绑定被取消. 过去的选项是一个变长的空零结尾的接口名称的字符串, 其最大长度为 IFNAMSIZ. 如果一个套接字被绑定至一接口, 只有由这个特定接口接收的信息包可以由此套接字处理.
SO_DEBUG
允许套接字调试.只对有 CAP_NET_ADMIN 功能或有效用户标识为 0 的进程有效.
SO_REUSEADDR
表示在一个 bind(2) 调用中对提供给它的地址使用的确认规则应该允许重复使用本地地址. 对于 PF_INET 套接字, 这表示该套接字可以绑定, 除非已有一个活跃的侦听套接口绑定到此地址上. 如果这个侦听套接字和一个指定端口绑定为 INADDR_ANY 时, 它就不能再绑定到任何本地地址的此端口.
SO_TYPE
按整数返回套接字类型(如 SOCK_STREAM) 只能通过 getsockopt 读取.
SO_DONTROUTE
不通过网关发送, 只能发送给直接连接的主机.可以通过在套接字的 send(2) 操作上设置 MSG_DONTROUTE 标志来实现相同的效果. 其值为布尔型整数的标识.
SO_BROADCAST
设置或获取广播标识. 当选择此选项时, 数据报套接字接收向广播地址发送的数据包, 并且可以向广播地址发送数据包. 这一选项对于面向流的套接字无效.
SO_SNDBUF
设置或得到套接字发送缓冲区的最大字节数. 其默认值由 wmem_default sysctl 设置,最大允许值由 wmem_max sysctl 设置.
SO_RCVBUF
设置或得到套接字接收缓冲区的最大字节数。其默认值由 rmem_default sysctl设置,最大允许值由 rmem_max sysctl 设置.
SO_LINGER
设置或获取 SO_LINGER 选项的值. 其参数为 linger 结构.
struct linger {
    int   l_onoff;    /* 延时状态(打开/关闭) */
    int   l_linger;   /* 延时多长时间 */
};

如果选择此选项, close(2) 或 shutdown(2) 将等到所有套接字里排队的消息成功发送或到达延迟时间后才会返回. 否则, 调用将立即返回. 而 closing 操作将在后台进行. 如果套接字是 exit(2), 的一部分关闭时, 它总是在后台延迟进行的.
SO_PRIORITY
设置在此套接字发送的所有包的协议定义优先权. Linux 通过这一值来排列网络队列: 根据所选设备排队规则, 具有更高优先权的包可以先被处理.对于 ip(7), 同时也设置了输出包的 IP 服务类型(TOS)的域.
SO_ERROR
取得并清除未解决的套接字错误. 只有在 getsockopt. 时有效. 是一个整数值.

SIGNALS

当向一个已关闭(被本地或远程终端)的面向联接的套接字写入时, 将向该写入进程发送 SIGPIPE 信号,并返回 EPIPE 如果写入命令声明了 MSG_NOSIGNAL 标识时, 不会发出此信号.

如果与 FIOCSETOWN fcntl 或 SIOCSPGRP ioctl 一起请求,那么当发生 I/O 事件时发出 SIGIO 这样我们就可以在信号句柄里使用 poll(2) 或 select(2) 找出发生事件的套接字. 另一种选择(在 Linux 2.2 中)是用 F_SETSIG fcntl 设置一个实时信号: 实时信号的处理程序被调用时还会收到它的 siginfo_tsi_fd 区域中的文件描述符. 更多信息参见 fcntl(2)

在某些环境中(例如:多个进程访问单个套接字), 引发 SIGIO 的东西在进程对信号作出反应时可能已经消失了. 如果这样的话, 进程应该再次等待, 因为 Linux 稍后会重发此信号.  

SYSCTLS

可以通过目录 /proc/sys/net/core/* 下的文件或者用 sysctl(2) 系统调用来访问内核套接字的网络系统控制(sysctl)信息.

rmem_default
指明套接字接收缓冲区的默认字节数.
rmem_max
指明套接字接收缓冲区的最大字节数, 用户可以通过使用 SO_RCVBUF 套接字选项来设置此值.
wmem_default
指明套接字发送缓冲区的默认字节数.
wmem_max
指明发送缓冲区的最大字节数,用户可以通过使用套接字的 SO_SNDBUF 选项来设置它的值.
message_costmessage_burst
设定记号存储桶过滤器, 在存储桶中保存一定数量的外部网络事件导致的警告消息.
netdev_max_backlog
在全局输入队列中包的最大数目.
optmem_max
每个套接字的象 iovecs 这样的辅助数据和用户控制数据的最大长度.

IOCTLS

以上的 IO 控制值可以通过 ioctl(2) 来访问:

error = ioctl(ip_socket, ioctl_type, &value_result);
SIOCGSTAMP
返回 timeval 类型的结构,其中包括有发送给用户的最后一个包接收时的时间戳。被用来测量精确的 RTT (round trip time) 时间. struct timeval. 结构说明请参考 setitimer(2)
SIOCSPGRP
在异步 IO 操作结束或者接收到紧急数据时,用来设置进程或进程组,向它(它们)发送 SIGIO 或者 SIGURG 信号, 参数为指向 pid_t. 类型的指针。如果参数为正,则发送信号到相应的进程。如果参数为负,则发送信号到此参数绝对值 id 所属的进程组的所有进程。如果它没有 CAP_KILL 功能或者它的有效 UID 不是 0, 进程只能选择它自己或自己的进程组来接收信号.
FIOASYNC
改变 O_ASYNC 标志来打开或者关闭套接字的异步 IO 模式。异步IO模式指的是:当新的 I/O 事件发生时,将发出 SIGIO 信号或者用 F_SETSIG 设置的信号.
参数为整形布尔量.
SIOCGPGRP
获得当前接收 SIGIO 或者 SIGURG 信号的进程或者进程组, 如果两个信号都没有设置, 则为 0.

有效的 fcntl:

FIOCGETOWN
与 IO 控制中的 SIOCGPGRP 相同.
FIOCSETOWN
与 IO 控制中的 SIOCSPGRP 相同.

注意

Linux 假设有一半的发送/接收缓冲区是用来处理内核结构, 因此, 系统控制的缓冲区是网络可访问的缓冲区的两倍.  

缺陷

CONFIG_FILTER 没有介绍 SO_ATTACH_FILTER SO_DETACH_FILTER 套接字选项. 在 libpcap 库有此接口的说明  

VERSIONS 版本

SO_BINDTODEVICE 在 Linux 2.0.30 中引入. SO_PASSCRED 是在 Linux 2.2 中引入的新选项. sysctl 是在 Linux 2.2. 中引入的新概念。  

作者

本手册页由 Andi Kleen 编写.

又见

socket(2), ip(7), setsockopt(2), getsockopt(2), packet(7), ddp(7)

#p#

NAME

socket - Linux socket interface  

SYNOPSIS

#include <sys/socket.h>
mysocket = socket(int socket_family, int socket_type, int protocol);

DESCRIPTION

This manual page describes the Linux networking socket layer user interface. The BSD compatible sockets are the uniform interface between the user process and the network protocol stacks in the kernel. The protocol modules are grouped into protocol families like PF_INET, PF_IPX, PF_PACKET and socket types like SOCK_STREAM or SOCK_DGRAM. See socket(2) for more information on families and types.

SOCKET LAYER FUNCTIONS

These functions are used by the user process to send or receive packets and to do other socket operations. For more information see their respective manual pages.

socket(2) creates a socket, connect(2) connects a socket to a remote socket address, the bind(2) function binds a socket to a local socket address, listen(2) tells the socket that new connections shall be accepted, and accept(2) is used to get a new socket with a new incoming connection. socketpair(2) returns two connected anonymous sockets (only implemented for a few local families like PF_UNIX)

send(2), sendto(2), and sendmsg(2) send data over a socket, and recv(2), recvfrom(2), recvmsg(2) receive data from a socket. poll(2) and select(2) wait for arriving data or a readiness to send data. In addition, the standard I/O operations like write(2), writev(2), sendfile(2), read(2), and readv(2) can be used to read and write data.

getsockname(2) returns the local socket address and getpeername(2) returns the remote socket address. getsockopt(2) and setsockopt(2) are used to set or get socket layer or protocol options. ioctl(2) can be used to set or read some other options.

close(2) is used to close a socket. shutdown(2) closes parts of a full duplex socket connection.

Seeking, or calling pread(2) or pwrite(2) with a non-zero position is not supported on sockets.

It is possible to do non-blocking IO on sockets by setting the O_NONBLOCK flag on a socket file descriptor using fcntl(2). Then all operations that would block will (usually) return with EAGAIN (operation should be retried later); connect(2) will return EINPROGRESS error. The user can then wait for various events via poll(2) or select(2).

I/O events
 
Event Poll flag Occurrence
 
Read POLLIN New data arrived.
 
Read POLLIN A connection setup has been completed (for connection-oriented sockets)
 
Read POLLHUP A disconnection request has been initiated by the other end.
 
Read POLLHUP A connection is broken (only for connection-oriented protocols). When the socket is written SIGPIPE is also sent.
 
Write POLLOUT Socket has enough send buffer space for writing new data.
 
Read/Write POLLIN|
POLLOUT
An outgoing connect(2) finished.
 
Read/Write POLLERR An asynchronous error occurred.
 
Read/Write POLLHUP The other end has shut down one direction.
 
Exception POLLPRI Urgent data arrived. SIGURG is sent then.
 

An alternative to poll/select is to let the kernel inform the application about events via a SIGIO signal. For that the FASYNC flag must be set on a socket file descriptor via fcntl(2) and a valid signal handler for SIGIO must be installed via sigaction(2). See the SIGNALS discussion below.  

SOCKET OPTIONS

These socket options can be set by using setsockopt(2) and read with getsockopt(2) with the socket level set to SOL_SOCKET for all sockets:

SO_KEEPALIVE
Enable sending of keep-alive messages on connection-oriented sockets. Expects a integer boolean flag.
SO_OOBINLINE
If this option is enabled, out-of-band data is directly placed into the receive data stream. Otherwise out-of-band data is only passed when the MSG_OOB flag is set during receiving.
SO_RCVLOWAT and SO_SNDLOWAT
Specify the minimum number of bytes in the buffer until the socket layer will pass the data to the protocol (SO_SNDLOWAT) or the user on receiving (SO_RCVLOWAT). These two values are not changeable in Linux and their argument size is always fixed to 1 byte. getsockopt is able to read them; setsockopt will always return ENOPROTOOPT.
SO_RCVTIMEO and SO_SNDTIMEO
Specify the receiving or sending timeouts until reporting an error. The parameter is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout has been reached then -1 is returned with errno set to EAGAIN or EWOULDBLOCK just as if the socket was specified to be nonblocking. If the timeout is set to zero (the default) then the operation will never timeout.
SO_BSDCOMPAT
Enable BSD bug-to-bug compatibility. This is used only by the UDP protocol module and scheduled to be removed in future. If enabled ICMP errors received for a UDP socket will not be passed to the user program. Linux 2.0 also enabled BSD bug-to-bug compatibility options (random header changing, skipping of the broadcast flag) for raw sockets with this option, but that has been removed in Linux 2.2. It is better to fix the user programs than to enable this flag.
SO_PASSCRED
Enable or disable the receiving of the SCM_CREDENTIALS control message. For more information see unix(7).
SO_PEERCRED
Return the credentials of the foreign process connected to this socket. Only useful for PF_UNIX sockets; see unix(7). Argument is a ucred structure. Only valid as a getsockopt.
SO_BINDTODEVICE
Bind this socket to a particular device like lqeth0rq, as specified in the passed interface name. If the name is an empty string or the option length is zero, the socket device binding is removed. The passed option is a variable-length null terminated interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an interface, only packets received from that particular interface are processed by the socket. Note that this only works for some socket types, particularly AF_INET sockets. It is not supported for packet sockets (use normal bind(8) there).
SO_DEBUG
Enable socket debugging. Only allowed for processes with the CAP_NET_ADMIN capability or an effective user id of 0.
SO_REUSEADDR
Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For PF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. When the listening socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this port for any local address.
SO_TYPE
Gets the socket type as an integer (like SOCK_STREAM). Can only be read with getsockopt.
SO_ACCEPTCONN
Returns a value indicating whether or not this socket has been marked to accept connections with listen(). The value 0 indicates that this is not a listening socket, the value 1 indicates that this is a listening socket. Can only be read with getsockopt.
SO_DONTROUTE
Don't send via a gateway, only send to directly connected hosts. The same effect can be achieved by setting the MSG_DONTROUTE flag on a socket send(2) operation. Expects an integer boolean flag.
SO_BROADCAST
Set or get the broadcast flag. When enabled, datagram sockets receive packets sent to a broadcast address and they are allowed to send packets to a broadcast address. This option has no effect on stream-oriented sockets.
SO_SNDBUF
Sets or gets the maximum socket send buffer in bytes. The default value is set by the wmem_default sysctl and the maximum allowed value is set by the wmem_max sysctl.
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The default value is set by the rmem_default sysctl and the maximum allowed value is set by the rmem_max sysctl.
SO_LINGER
Sets or gets the SO_LINGER option. The argument is a linger structure.
struct linger {
    int   l_onoff;    /* linger active */
    int   l_linger;   /* how many seconds to linger for */
};

When enabled, a close(2) or shutdown(2) will not return until all queued messages for the socket have been successfully sent or the linger timeout has been reached. Otherwise, the call returns immediately and the closing is done in the background. When the socket is closed as part of exit(2), it always lingers in the background.
SO_PRIORITY
Set the protocol-defined priority for all packets to be sent on this socket. Linux uses this value to order the networking queues: packets with a higher priority may be processed first depending on the selected device queueing discipline. For ip(7), this also sets the IP type-of-service (TOS) field for outgoing packets.
SO_ERROR
Get and clear the pending socket error. Only valid as a getsockopt. Expects an integer.

SIGNALS

When writing onto a connection-oriented socket that has been shut down (by the local or the remote end) SIGPIPE is sent to the writing process and EPIPE is returned. The signal is not sent when the write call specified the MSG_NOSIGNAL flag.

When requested with the FIOSETOWN fcntl or SIOCSPGRP ioctl, SIGIO is sent when an I/O event occurs. It is possible to use poll(2) or select(2) in the signal handler to find out which socket the event occurred on. An alternative (in Linux 2.2) is to set a realtime signal using the F_SETSIG fcntl; the handler of the real time signal will be called with the file descriptor in the si_fd field of its siginfo_t. See fcntl(2) for more information.

Under some circumstances (e.g. multiple processes accessing a single socket), the condition that caused the SIGIO may have already disappeared when the process reacts to the signal. If this happens, the process should wait again because Linux will resend the signal later.  

SYSCTLS

The core socket networking sysctls can be accessed using the /proc/sys/net/core/* files or with the sysctl(2) interface.

rmem_default
contains the default setting in bytes of the socket receive buffer.
rmem_max
contains the maximum socket receive buffer size in bytes which a user may set by using the SO_RCVBUF socket option.
wmem_default
contains the default setting in bytes of the socket send buffer.
wmem_max
contains the maximum socket send buffer size in bytes which a user may set by using the SO_SNDBUF socket option.
message_cost and message_burst
configure the token bucket filter used to load limit warning messages caused by external network events.
netdev_max_backlog
Maximum number of packets in the global input queue.
optmem_max
Maximum length of ancillary data and user control data like the iovecs per socket.

IOCTLS

These ioctls can be accessed using ioctl(2):

error = ioctl(ip_socket, ioctl_type, &value_result);
SIOCGSTAMP
Return a struct timeval with the receive timestamp of the last packet passed to the user. This is useful for accurate round trip time measurements. See setitimer(2) for a description of struct timeval.
SIOCSPGRP
Set the process or process group to send SIGIO or SIGURG signals to when an asynchronous I/O operation has finished or urgent data is available. The argument is a pointer to a pid_t. If the argument is positive, send the signals to that process. If the argument is negative, send the signals to the process group with the id of the absolute value of the argument. The process may only choose itself or its own process group to receive signals unless it has the CAP_KILL capability or an effective UID of 0.
FIOASYNC
Change the O_ASYNC flag to enable or disable asynchronous IO mode of the socket. Asynchronous IO mode means that the SIGIO signal or the signal set with F_SETSIG is raised when a new I/O event occurs.
Argument is a integer boolean flag.
SIOCGPGRP
Get the current process or process group that receives SIGIO or SIGURG signals, or 0 when none is set.

Valid fcntls:

FIOGETOWN
The same as the SIOCGPGRP ioctl.
FIOSETOWN
The same as the SIOCSPGRP ioctl

NOTES

Linux assumes that half of the send/receive buffer is used for internal kernel structures; thus the sysctls are twice what can be observed on the wire.  

BUGS

The CONFIG_FILTER socket options SO_ATTACH_FILTER and SO_DETACH_FILTER are not documented. The suggested interface to use them is via the libpcap library.  

VERSIONS

SO_BINDTODEVICE was introduced in Linux 2.0.30. SO_PASSCRED is new in Linux 2.2. The sysctls are new in Linux 2.2. SO_RCVTIMEO and SO_SNDTIMEO are supported since Linux 2.3.41. Earlier, timeouts were fixed to a protocol specific setting, and could not be read or written.  

SEE ALSO

socket(2), ip(7), setsockopt(2), getsockopt(2), packet(7), ddp(7)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-15 10:21:09

man中文man

2011-08-24 16:48:36

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-12 14:58:05

killall中文man

2011-07-15 16:58:36

ac中文man

2011-08-15 11:10:48

more中文man

2011-08-25 17:03:51

pclose中文man

2011-08-15 14:10:37

tar中文man

2011-08-16 10:42:30

rmmod中文man

2011-08-11 17:46:02

chmod中文man

2011-08-18 13:57:38

acct中文man

2011-08-23 17:49:36

zdump中文man

2011-08-15 15:10:49

wall中文man

2011-08-12 13:26:56

groff中文man

2011-08-25 11:39:13

tzfile中文man

2011-08-23 15:22:52

rootflags中文man

2011-08-23 15:06:03

quotastats中文man

2011-08-15 17:35:50

ar中文man

2011-08-25 09:07:16

suffixes中文man
点赞
收藏

51CTO技术栈公众号