bind 翻译

BIND(2)                                       Linux Programmer's Manual                                       BIND(2)

NAME
       bind - bind a name to a socket		/*把一个 name 和 socket 绑定*/

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int bind(int sockfd, const struct sockaddr *addr,
                socklen_t addrlen);

DESCRIPTION
       When  a  socket  is  created  with  socket(2),  it  exists in a name space (address family) but has no address assigned to it.  bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd.   addrlen  specifies  the size, in bytes, of the address structure pointed to by addr.  Traditionally, this operation is called “assigning a name to a socket”.
	   /*当 socket 函数创建一个 socket,这仅仅只是占了一个 name space(地址里有一个 socket)而并没有分配地址给它。bind 函数就是分配一个 addr 指向的地址给由 socket 创建的文件描述符 sockfd。参数 addrlen 的字节大小就是 addr 指向的 address 结构体的大小。一般地,我们称这种操作叫“分配一个 name 给 socket”*/

       It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may  receive  connections (see accept(2)).
	   /*通常在 SOCK_STREAM 套接字接收连接前都要用 bind 函数分配一个本本地地址(参见 accept 函数)*/

       The  rules  used  in  name binding vary between address families.  Consult the manual entries in Section 7 for detailed information.  For AF_INET see  ip(7),  for  AF_INET6  see  ipv6(7),  for  AF_UNIX  see  unix(7),  for AF_APPLETALK see ddp(7), for AF_PACKET see packet(7), for AF_X25 see x25(7) and for AF_NETLINK see netlink(7).
	   /*在 name 绑定使用的规则会因地址族而异。有关详细信息,请参阅第7部分中的手册条目。AF_INET 参见 ip(7),AF_INET6 参见 ipv6(7),AF_UNIX 参见 unix(7),AF_APPLETALK 参见ddp(7),AF_PACKET 参加 packet(7),AF_X25 参见 x25(7) 以及 AF_NETLINK 参见 netlink(7)*/

       The  actual  structure passed for the addr argument will depend on the address family.  The sockaddr structure is defined as something like:
	   /*对于 addr 参数传递的实际结构将取决于 addr 指向的地址族。sockaddr 结构体的定义如下*/

           struct sockaddr {
               sa_family_t sa_family;
               char        sa_data[14];
           }

       The only purpose of this structure is to cast the structure pointer passed in addr in order to avoid  compiler warnings.  See EXAMPLE below.
	   /*这个结构的唯一目的是转换在 addr 中传递的结构指针,以避免编译器警告*/

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
	   /*成功返回 0,出错返回 -1,errno 的值表示出错代码*/

ERRORS
       EACCES The address is protected, and the user is not the superuser.
	   /*EACCES		地址受保护,并且当前用户并不是超级用户*/

       EADDRINUSE The given address is already in use.
	   /*EADDRINUSE		给定的地址已经被使用过了*/
			  

       EADDRINUSE (Internet  domain  sockets) The port number was specified as zero in the socket address structure, but, upon attempting to bind to an ephemeral port, it was determined that all port numbers in the  ephemeral port range are currently in use.  See the discussion of /proc/sys/net/ipv4/ip_local_port_range ip(7).
	   /*EADDRINUSE		(网络域套接字)端口号在套接字地址结构中被指定为零,但是,在试图绑定到临时端口时发现所有的临时端口范围内的端口号当前都被使用了*/

       EBADF  sockfd is not a valid descriptor.
	   /*EBADF			参数 sockfd 并不是一个有效的套接字描述符*/

       EINVAL The socket is already bound to an address.
	   /*EINVAL			参数 sockfd 已经绑定了一个地址*/

       EINVAL addrlen is wrong, or addr is not a valid address for this socket's domain.
	   /*EINVAL			地址长度错误或者 addr 并不是一个当前套接字域的有效地址*/

       ENOTSOCK The file descriptor sockfd does not refer to a socket.
	   /*ENOTSOCK		参数sockfd 并不是一个套接字文件描述符*/

       The following errors are specific to UNIX domain (AF_UNIX) sockets:
	   /*下面的错误特指 UNIX 域(AF_UNIX)套接字*/

       EACCES Search permission is denied on a component of the path prefix.  (See also path_resolution(7).)
	   /*EACCES			搜索权限在路径前缀的组件上被拒绝*/

       EADDRNOTAVAIL A nonexistent interface was requested or the requested address was not local.
	   /*EADDRNOTAVAIL	请求不存在的接口或请求的地址不是本地的*/

       EFAULT addr points outside the user's accessible address space.
	   /*EFAULT			addr 指向一个用户访问不到的地址空间*/

       ELOOP  Too many symbolic links were encountered in resolving addr.
	   /*ELOOP			解析 addr 时遇到过多符号链接*/

       ENAMETOOLONG addr is too long.
	   /*ENAMETOOLONG	地址太长*/

       ENOENT The file does not exist.
	   /*ENOENT			文件不存在*/

       ENOMEM Insufficient kernel memory was available.
	   /*ENOMEM 		内核内存不足*/

       ENOTDIR A component of the path prefix is not a directory.
	   /*ENOTDIR		路径前缀的组件不是目录*/

       EROFS  The socket inode would reside on a read-only filesystem.
	   /*EROFS			socket inode 是一个只读文件系统*/

CONFORMING TO
       POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (bind() first appeared in 4.2BSD).
	   /*bind 函数第一次出现在 BSD 4.2 版本中*/

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  third  argument of bind() is in reality an int (and this is what 4.x BSD and libc4 and libc5 have).  Some POSIX confusion resulted in the present socklen_t, also used by glibc.  See also accept(2).
	   /*bind 函数的第三个参数实际上就是 int 类型的。一些 POSIX 标准混淆导致了现在使用的 socklen_t,但也被 glibc 使用*/

BUGS
       The transparent proxy options are not described.
	   /*一些易懂可替换的选项没有被描述出来*/

EXAMPLE
       An example of the use of bind() with Internet domain sockets can be found in getaddrinfo(3).
	   /*在 getsddrinfo 函数中有关于网络域套接字使用的例子*/

       The following example shows how to bind a stream socket in the UNIX (AF_UNIX) domain, and accept connections:
	   /*下面例子展示如何绑定一个流式 AF_UNIX 域套接字以及如何接收连接的*/

       #include <sys/socket.h>
       #include <sys/un.h>
       #include <stdlib.h>
       #include <stdio.h>
       #include <string.h>

       #define MY_SOCK_PATH "/somepath"
       #define LISTEN_BACKLOG 50

       #define handle_error(msg) \
           do { perror(msg); exit(EXIT_FAILURE); } while (0)

       int
       main(int argc, char *argv[])
       {
           int sfd, cfd;
           struct sockaddr_un my_addr, peer_addr;
           socklen_t peer_addr_size;

           sfd = socket(AF_UNIX, SOCK_STREAM, 0);
           if (sfd == -1)
               handle_error("socket");

           memset(&my_addr, 0, sizeof(struct sockaddr_un));
                               /* Clear structure */
           my_addr.sun_family = AF_UNIX;
           strncpy(my_addr.sun_path, MY_SOCK_PATH,
                   sizeof(my_addr.sun_path) - 1);

           if (bind(sfd, (struct sockaddr *) &my_addr,
                   sizeof(struct sockaddr_un)) == -1)
               handle_error("bind");

           if (listen(sfd, LISTEN_BACKLOG) == -1)
               handle_error("listen");

           /* Now we can accept incoming connections one
              at a time using accept(2) */

           peer_addr_size = sizeof(struct sockaddr_un);
           cfd = accept(sfd, (struct sockaddr *) &peer_addr,
                        &peer_addr_size);
           if (cfd == -1)
               handle_error("accept");

           /* Code to deal with incoming connection(s)... */

           /* When no longer required, the socket pathname, MY_SOCK_PATH
              should be deleted using unlink(2) or remove(3) */
       }

SEE ALSO
       accept(2), connect(2), getsockname(2), listen(2), socket(2), getaddrinfo(3),  getifaddrs(3),  ip(7),  ipv6(7),
       path_resolution(7), socket(7), unix(7)

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                                              BIND(2)

猜你喜欢

转载自blog.csdn.net/wenfei11471/article/details/80059063
今日推荐