Linux socket socket(2) (翻译 man 2)

来源:互联网 发布:omgvsig淘宝权瞎子 编辑:程序博客网 时间:2024/05/19 12:40
SOCKET(2)                     Linux Programmer's Manual                     SOCKET(2)

NAME

       socket - create an endpoint for communication
//为通讯创建一个端点(译者注:endpoint 即为服务器端和客户端的“端”的意思)

SYNOPSIS

       #include           /* See NOTES */       #include        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 .  The currently understood formats include://domain 参数指定通信的域;这是来选择将被用来通信的协议族。这些协议族在中定义,当前明确的格式包括:       Name                Purpose                          Man page       AF_UNIX, AF_LOCAL   Local communication              unix(7)       AF_INET             IPv4 Internet protocols          ip(7)       AF_INET6            IPv6 Internet protocols          ipv6(7)       AF_IPX              IPX - Novell protocols       AF_NETLINK          Kernel user interface device     netlink(7)       AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)       AF_AX25             Amateur radio AX.25 protocol       AF_ATMPVC           Access to raw ATM PVCs       AF_APPLETALK        Appletalk                        ddp(7)       AF_PACKET           Low level packet interface       packet(7)       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.//提供有序的,可信任的,双向的,基于连接的字节流。(译者注:byte繁体一般翻译为位元组,意思是8个位元组成的数据,也就是8个bit(位元)构成1个byte;简体中文翻译为字节;
//但是bit的发音和byte比较接近,比特指的是bit,而字节指的是byte。这方面繁体中文,主要是台湾翻译的好一些,但是大陆可能就听不懂了,这里给出说明)。
//可能支持越界的数据传输策略。       SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages of a                       fixed maximum length).//支持数据报(无连接的,不可信任的,固定最大长度的不可靠的信息)(译者注:报实际上是电报的意思,那肯定是只管发送,至于接受就不保证了)       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_RAW        Provides raw network protocol access.//提供原始网络协议访问。       SOCK_RDM        Provides a reliable datagram layer that does not guarantee                       ordering.//提供一个不保证顺序的可信任的数据报层。       SOCK_PACKET     Obsolete and should not be used in new programs; see                       packet(7).//过时的不应在新程序中使用,参见packet(7)       Some socket types may not be implemented by all protocol families; for       example, SOCK_SEQPACKET is not implemented for AF_INET.//一些socket类型并没用被所以的协议族实现;例如,SOCK_SEQPACKET就没有在AF_INET上实现。       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 type参数提供第二个用途:除了指定socket类型之外,type还可以包含按位与的以下值来修改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.//在新打开的文件描述符上设置O_NONBLOCK 文件状态,使用这个标志位来保存对fcntl(2)的额外调用来完成相同的结果。       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.//在新打开的文件描述符上设置FD_CLOEXEC标志位,参见open(2)中对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可以指定为0.然而,有可能很多协议都会存在,在这种情况下特定的协议必须在这种方式下指定。
//使用的协议号指定为通信的时候被取代的“通信域”;参见protocol(5)。参见getprotoent(3)来如何映射协议名称字符到协议号。       Sockets of type SOCK_STREAM are full-duplex byte streams, similar to pipes.       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 类型的socket(译者注:两端)是全双工的字节流,和管道类似。两端的socket都不暴力记录的边界。
//在流式的socket上发送和接受数据之前,socket必须为已经连接的状态。对另一个socket的连接是有connect(2)调用创建的。
//一旦连接,数据可以使用read(2)和 write(2)调用或者是send(2)和recv(2)的变种来传输。当会话完成的时候,
//可能会调用close(2)。越界的数据也会被发送和接受,就像send(2)和recv(2)中描述的那样。       The communic ations 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 的通信行为协议确保数据不会丢失或重复。在一个有问题的时间长度内,如果对等协议的缓存空间中的部分数据不能成功的传送,
//那么连接就认为已经挂了。当在socket上使能SO_KEEPALIVE 标志的时候,协议会检查在协议指定的行为内是否其它的端点还是活动的。
//SIGPIPE 信号会产生在如果进程在一个损坏的流上发送或接受的情况下;这会导致本机的进程不处理这个信号并退出。
//SOCK_SEQPACKET socket和SOCK_STREAM socket使用相同的系统调用,唯一的不同是read(2)调用会返回需要的数据数量,同时在到达的包中的任何数据都会被丢弃。
//在接收到的数据报中所有的消息边界都会被保存。       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_DGRAMSOCK_RAW 类型的socket允许发送在sendto(2)调用中命令的通信发送数据报。数据报一般有recvfrom(2)来接收,
//recvfrom会返回下一个数据报和它发送者的地址。       SOCK_PACKET is an obsolete socket type to receive raw packets directly from       the device driver.  Use packet(7) instead.//SOCK_PACKET 是一个过时的socket类型,用来直接从设备驱动接受原始包数据。使用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(2)的F_SETOWN 操作可以用来指定进程或者进程组来接受 SIGURG 信号,当接收到过界数据或者接收到当SOCK_STREAM连接异常中断是的SIGPIPE的时候.
//F_SETOWN 操作还可以被用来设置进程或者进程组通过SIGIO来接受I/O和异步的I/O事件的通知.
//使用F_SETOWN 和ioctl(2)调用 FIOSETOWN 和 SIOCSPFRP 是等效的.       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使用IGMP信息),那就对socket设置未定的标志位.
//在这个socket上的下个操作将会返回未定错误的错误码.对一些协议而言,有可能通过使能预socket错误队列的形式来取回错误的细节,在ip(7)参见 IP_RECVERR.       The operation of sockets is controlled by socket level options.  These options       are defined in .  The functions setsockopt(2) and getsockopt(2)       are used to set and get options, respectively.
//socket的操作是有socket的level 操作选项的.这些选项在中定义,函数 setsockopt(2) 和 getsockopt(2)分别来设置和获取选项.

RETURN VALUE

       On success, a file descriptor for the new socket is returned.  On error, -1 is       returned, and errno is set appropriately.
//成功的话会返回新socket的文件描述符,错误的话返回-1,errno自动设置.

ERRORS

       EACCES Permission to create a socket of the specified type and/or protocol is              denied.//创建指定类型和/或协议的socket权限禁止.       EAFNOSUPPORT              The implementation does not support the specified address family.//程序不支持指定的地址族.       EINVAL Unknown protocol, or protocol family not available.//未知协议,或者协议族无效.       EINVAL Invalid flags in type.//type标志位无效       EMFILE Process file table overflow.//处理文件表溢出.       ENFILE The system limit on the total number of open files has been reached.//已经达到了系统打开文件数的最大值       ENOBUFS or ENOMEM              Insufficient memory is available.  The socket cannot be created until              sufficient resources are freed.//有效内存不足.不能创建socket直到充足的资源释放.       EPROTONOSUPPORT              The protocol type or the specified protocol is not supported within              this domain.//在域中不支持协议类型或者是指定的协议       Other errors may be generated by the underlying protocol modules.
//其它错误可能由基本的协议模块产生.

CONFORMING TO

       4.4BSD, POSIX.1-2001.       The SOCK_NONBLOCK and SOCK_CLOEXEC flags are Linux-specific.//SOCK_NONBLOCKSOCK_CLOEXEC 是Linux指定的       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).
//socket()出现在4.2的BSD中,渐渐的在非BSD系统上socket层的副本也是可移植的(包含 System V的变种).

NOTES

       POSIX.1-2001 does not require the inclusion of , 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-2001 不需要包含 ,并且这个头文件在Linux上也是不需要的.但是一些历史上的(BSD)程序需要这个头文件,
//可移植的程序最好还是包含它.       The manifest constants used under 4.x BSD for protocol families are PF_UNIX,       PF_INET, etc., while AF_UNIX etc. 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 等,作为地址族使用.
//然后BSD的man 页面许诺:协议族要逐渐和地址族相同,后续的标准在哪里都使用AF_* 

EXAMPLE

       An example of the use of socket() is shown in getaddrinfo(3).

SEE ALSO

       accept(2), bind(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" is reprinted in       UNIX Programmer's Supplementary Documents Volume 1.       "BSD Interprocess Communication Tutorial" is reprinted in UNIX Programmer's       Supplementary Documents Volume 1.

COLOPHON

       This page is part of release 3.31 of the Linux man-pages project.  A       description of the project, and information about reporting bugs, can be found       at http://www.kernel.org/doc/man-pages/.Linux                                 2009-01-19                            SOCKET(2)