SOCKET(2) Linux Programmer's Manual SOCKET(2) NAME socket - create an endpoint for communication /*创建一个通信接口*/ SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int socket(int domain, int type, int protocol); DESCRIPTION socket() creates an endpoint for communication and returns a descriptor. /*socket 函数创建一个通信接口并返回一个描述符*/ 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 currently understood formats include: /*domain 参数指定一个通信域(如 IPV4、IPV6等);domain 参数的选择将会用于通信的协议族。这些协议族在 <sys/socket.h> 中被定义。目前关于 domain 参数能够被理解的值包括一下*/ Name Purpose Man page AF_UNIX, AF_LOCAL Local communication unix(7) /*对应于本地通信*/ AF_INET IPv4 Internet protocols ip(7) /*对应于 IPV4 网络通信协议*/ AF_INET6 IPv6 Internet protocols ipv6(7) /*对应于 IPV6 网络通信协议*/ AF_IPX IPX - Novell protocols /*对应于 IPX-Novell 协议*/ AF_NETLINK Kernel user interface device netlink(7) /*对应于内核用户界面设备*/ AF_X25 ITU-T X.25 / ISO-8208 protocol x25(7) /*对应于 ITU-T X.25 / ISO-8208 协议*/ AF_AX25 Amateur radio AX.25 protocol /*对应于非专业性无线电 AX.25 协议*/ AF_ATMPVC Access to raw ATM PVCs /*对应于获取原始 ATM PVCs*/ AF_APPLETALK AppleTalk ddp(7) /*对应于 AppleTalk*/ AF_PACKET Low level packet interface packet(7) /*对应于底层数据包接口*/ AF_ALG Interface to kernel crypto API /*对应于内核加密 API 接口*/ The socket has the indicated type, which specifies the communication semantics. Currently defined types are: /*socket 具有执行的类型,它规定了通信的语义。目前已经定义的类型有以下:*/ SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported. /*SOCK_STREAM 提供有序、可靠、双向、基于连接的字节流。可以支持带外数据传输机制 -------> TCP*/ SOCK_DGRAM Supports datagrams (connectionless, unreliable messages of a fixed maximum length). /*SOCK_DGRAM 支持数据报(无连接、不可靠、具有固定最大长度的消息) -------> UDP*/ SOCK_SEQPACKET Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call. /*SOCK_SEQPACKET 为固定最大长度的数据报提供有序,可靠的基于双向连接的数据传输路径; 消费者每次输入系统调用时都要读取整个数据包*/ SOCK_RAW Provides raw network protocol access. /*SOCK_RAW 提供原始网络协议访问。*/ SOCK_RDM Provides a reliable datagram layer that does not guarantee ordering. /*SOCK_RDM 提供不保证顺序的可靠数据报层*/ SOCK_PACKET Obsolete and should not be used in new programs; see packet(7). /*SOCK_PACKET 已经废弃不应在新程序中使用*/ Some socket types may not be implemented by all protocol families. /*有些套接字类型可能无法通过现有的协议族实现*/ Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the bitwise OR of any of the following values, to modify the behavior of socket(): /*从 Linux 2.6.27 开始,类型参数有第二个目的:除了指定套接字类型之外,它还可能包含以下任何值的按位或运算符,以修改 socket 函数的行为*/ SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the new open file description. Using this flag saves extra calls to fcntl(2) to achieve the same result. /*SOCK_NONBLOCK 在新的打开文件描述符中设置 O_NONBLOCK 文件状态标志。使用此标志可以减少对 fcntl 函数的额外调用,使用 fcntl 函数同样可以实现以上功能*/ SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful. /*SOCK_CLOEXEC 在新文件描述符上设置 close-on-exec(FD_CLOEXEC)标志。想了解该标志位的用处请参阅 open 函数中对 O_CLOEXEC 标志的说明*/ 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. 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. /*protocol 参数是用于把指定协议和 socket 关联起来,通常给定的协议族所对应的 socket 的 protocol 参数只有一个,此时 protocol 参数填 0;如果对应的 protocol 参数值有很多, 那么就要根据不同情况设定不同的值了。protocol 参数值用于指定的通信中来代替通信,见 protocols(5)。想了解如何将协议名称与 protocol 参数值匹配起来的请参阅 getprotoent(3)*/ Sockets of type SOCK_STREAM are full-duplex byte streams. They do not preserve record boundaries. A stream socket must be in a connected state before any data may be sent or received on it. A connection to another socket is created with a connect(2) call. Once connected, data may be transferred using read(2) and write(2) calls or some variant of the send(2) and recv(2) calls. When a session has been completed a close(2) may be performed. Out-of-band data may also be transmitted as described in send(2) and received as described in recv(2). /*SOCK_STREAM 类型的套接字表示全双工字节流。 他们不保留记录界限,在发送或接收任何数据之前, 流套接字必须处于连接状态,当然我们可以通过调用 connect 函数来建立两个套接字的连接。一旦建立连接,我们可以调用 read/write 函数或者一些其他的如:send/recv 等函数来传输数据。当一个会话完成了,我们可以调用 close 函数来关闭套接字。同样也可以用 send/recv 函数来发送/接收带外数据*/ The communications protocols which implement a SOCK_STREAM ensure that data is not lost or duplicated. If a piece of data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, then the connection is considered to be dead. When SO_KEEPALIVE is enabled on the socket the protocol checks in a protocol-specific manner if the other end is still alive. 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. 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. /*使用 SOCK_STREAM 作为通信协议时能确保数据不会丢失或重复。如果在对等协议空间中有一块数据在合理的时间内没能传输成功,那么就认为连接断开了。当在套接字上启用SO_KEEPALIVE时,protocol 参数会通过协议特定的方式检查另一端是否仍然存在。如果进程发送或接收到一个已损坏的流,则会触发 SIGPIPE 信号,如果没有设置对该信号的处理函数,那么会导致进程意外终止。SOCK_SEQPACKET 套接字和 SOCK_STREAM 套接字几乎相同,唯一区别在于 SOCK_SEQPACKET 套接字在是由 read 函数时只会返回请求的数据量,剩余的部分就会被丢弃了。和 SOCK_STREAM 套接字一样传入数据报中的所有消息边界都将保留*/ SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to correspondents named in sendto(2) calls. Datagrams are generally received with recvfrom(2), which returns the next datagram along with the address of its sender. /*SOCK_DGRAM 和SOCK_RAW 套接字允许给 sendto 函数中的通讯对象发送数据报*/ SOCK_PACKET is an obsolete socket type to receive raw packets directly from the device driver. Use packet(7) instead. /*SOCK_PACKET 是一种废弃了的套接字类型,用于直接从设备驱动程序接收原始数据包,已经被 packet(7) 代替了*/ 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. /* fcntl 函数的 F_SETOWN 操作可以用来指定进程或进程组来接收外部数据到来时产生的 SIGURG 信号以及 SOCK_STREAM 连接意外中断所产生的 SIGPIPE 信号。fcntl 函数的 F_SETOWN 操作也可以用来设置进程或进程组,该进程或进程组用于接收由 SIGIO 产生的 I/O 和异步 I/O 事件通告*/ When the network signals an error condition to the protocol module (e.g., using a ICMP message for IP) the pending error flag is set for the socket. The next operation on this socket will return the error code of the pending error. For some protocols it is possible to enable a per-socket error queue to retrieve detailed information about the error; see IP_RECVERR in ip(7). /*当网络向协议模块发出一个错误条件信号(例如,使用IP的ICMP消息)时,套接字的 pending error 标志位将会被激活。此套接字上的下一个操作将会返回 pending error 位的错误代码。对于某些协议,可以使用 per-socket error queue 来检索有关错误的详细信息*/ The operation of sockets is controlled by socket level options. These options are defined in <sys/socket.h>. The functions setsockopt(2) and getsockopt(2) are used to set and get options, respectively. /*套接字的操作由套接字级选项控制。这些选项被定义在 <sys/socket.h>中。函数setsockopt 和 getsockopt 分别用于设置和获取该选项*/ RETURN VALUE On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errno is set appropriately. /*成功返回新套接字的文件描述符。出错返回 -1,同时错误代码会被设置在变量 errno 中*/ ERRORS EACCES Permission to create a socket of the specified type and/or protocol is denied. /*EACCES 创建套接字的指定类型和/或套接字的协议限不足*/ EAFNOSUPPORT The implementation does not support the specified address family. /*EAFNOSUPPORT 不支持指定的地址系列*/ EINVAL Unknown protocol, or protocol family not available. /*EINVAL 未知的协议或者协议族不可用*/ EINVAL Invalid flags in type. /*EINVAL 类型中的标志无效*/ EMFILE The per-process limit on the number of open file descriptors has been reached. /*EMFILE 已达到进程打开文件描述符数量的上限*/ ENFILE The system-wide limit on the total number of open files has been reached. /*ENFILE 已达到系统范围内打开文件总数的上限*/ ENOBUFS or ENOMEM Insufficient memory is available. The socket cannot be created until sufficient resources are freed. /*ENOBUFS or ENOMEM 内存不足。只有释放足够的资源后才能创建套接字*/ EPROTONOSUPPORT The protocol type or the specified protocol is not supported within this domain. /*EPROTONOSUPPORT 此域中不支持协议类型或指定的协议*/ Other errors may be generated by the underlying protocol modules. /*其他错误可能由底层协议模块生成*/ CONFORMING TO POSIX.1-2001, POSIX.1-2008, 4.4BSD. The SOCK_NONBLOCK and SOCK_CLOEXEC flags are Linux-specific. socket() appeared in 4.2BSD. It is generally portable to/from non-BSD systems supporting clones of the BSD socket layer (including System V variants). NOTES POSIX.1 does not require the inclusion of <sys/types.h>, and this header file is not required on Linux. However, some historical (BSD) implementations required this header file, and portable applications are probably wise to include it. /* POSIX.1 不需要 <sys/types.h>头文件,但是由于历史原因(例如:BSD)就需要该头文件,所有还是加一下该头文件比较好*/ The manifest constants used under 4.x BSD for protocol families are PF_UNIX, PF_INET, and so on, while AF_UNIX, AF_INET, and so on are used for address families. However, already the BSD man page promises: "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere. /*4.x BSD 下常量 PF_UNIX, PF_INET 等用于协议族,而 AF_UNIX, AF_INET 等用于地址族。 然而,BSD手册页已经承诺:“协议族通常与地址族相同”,因此随后所有的标准在都使用AF_ **/ The AF_ALG protocol type was added in Linux 2.6.38. More information on this interface is provided with the kernel HTML documentation at https://www.kernel.org/doc/htmldocs/crypto-API/User.html. /*AF_ALG 协议类型是在Linux 2.6.38中添加的。关于此接口的更多信息,请参阅https://www.kernel.org/doc/htmldocs/crypto-API/User.html上的内核 HTML 文档*/ EXAMPLE An example of the use of socket() is shown in getaddrinfo(3). /* getaddrinfo(3) 中有关于 socket 的例子*/ SEE ALSO accept(2), bind(2), close(2), connect(2), fcntl(2), getpeername(2), getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2), select(2), send(2), shutdown(2), socketpair(2), write(2), getprotoent(3), ip(7), socket(7), tcp(7), udp(7), unix(7) “An Introductory 4.3BSD Interprocess Communication Tutorial” and “BSD Interprocess Communication Tutorial”, reprinted in UNIX Programmer's Supplementary Documents Volume 1. COLOPHON This page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man-pages/. Linux 2015-12-28 SOCKET(2)
socket 翻译
猜你喜欢
转载自blog.csdn.net/wenfei11471/article/details/80057323
今日推荐
周排行