NAME 名称
bind - 将一个名字和一个套接字绑定到一起(赋一个名字给一个套接字)
SYNOPSIS 概述
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
DESCRIPTION 描述
bind 为套接字 sockfd 指定本地地址 my_addr. my_addr 的长度为 addrlen (字节).传统的叫法是给一个套接字分配一个名字. 当使用 socket(2), 函数创建一个套接字时,它存在于一个地址空间(地址族), 但还没有给它分配一个名字
一般来说在使用 SOCK_STREAM 套接字建立连接之前总要使用 bind 为其分配一个本地地址.参见 accept(2)).
NOTES 注意
这条规则用于给每个地址族绑定不同的名称.更多细节请参考手册页第7册(man7). 对于 AF_INET 参见 ip(7), 对于 AF_UNIX 参见 unix(7), 对于 AF_APPLETALK 参见 ddp(7), 对于 AF_PACKET 参见 packet(7), 对于r AF_X25 参见 x25(7) 对于 AF_NETLINK 参见 netlink(7).
RETURN VALUE 返回值
函数执行成功返回0,否则返回-1, 并设置错误代码.
ERRORS 错误
- EBADF
- sockfd 不是一个合法套接字描述符.
- EINVAL
- 套接字已经绑定到一个地址.这一条在以后会有所改变: 具体参见 linux/unix/sock.c
- EACCES
- 地址受保护,用户不是系统管理员.
- ENOTSOCK
- 参数是文件描述符,不是一个套接字.
下列错误适用于UNIX域 (AF_UNIX) 套接字.
- EINVAL
- 地址长度 addrlen 错误,或者套接字不在 AF_UNIX 族.
- EROFS
- 套接字节点位于只读文件系统.
- EFAULT
- my_addr 指向用户无权访问的地址空间.
- ENAMETOOLONG
- my_addr 长度超范围.
- ENOENT
- 文件不存在.
- ENOMEM
- 内核存储空间不足.
- ENOTDIR
- 指定路径不是一个目录.
- EACCES
- 指定路径拒绝访问.
- ELOOP
- 在解析 my_addr 时发现过多符号连接.
BUGS 勘误
透明代理选项没有描述.
CONFORMING TO 一致性
SVr4,4.4BSD(函数 bind 首次出现于BSD 4.2)SVr4文档增加了 EADDRNOTAVAIL, EADDRINUSE, 和 ENOSR 一般性错误, 还增加了 EIO, EISDIR 和 EROFS Unix域错误.
NOTE
函数 bind 的第三个参数实际上是int类型(BSD 4.*和libc4以及libc5都是这么做的). 不知为什么有的POSIX系统目前仍在使用socklen_t. 目前尚无统一标准,不过glibc2两者都使用.参见 accept(2).
SEE ALSO 参见
accept(2), connect(2), listen(2), socket(2), getsockname(2), ip(7), socket(7)
#p#
NAME
bind - bind a name to a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
DESCRIPTION
bind gives the socket sockfd the local address my_addr. my_addr is addrlen bytes long. Traditionally, this is called lqassigning a name to a socket.rq When a socket is created with socket(2), it exists in a name space (address family) but has no name assigned.
It is normally necessary to assign a local address using bind before a SOCK_STREAM socket may receive connections (see accept(2)).
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_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).
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
ERRORS
- EBADF
- sockfd is not a valid descriptor.
- EINVAL
- The socket is already bound to an address. This may change in the future: see linux/unix/sock.c for details.
- EACCES
- The address is protected, and the user is not the super-user.
- ENOTSOCK
- Argument is a descriptor for a file, not a socket.
The following errors are specific to UNIX domain (AF_UNIX) sockets:
- EINVAL
- The addrlen is wrong, or the socket was not in the AF_UNIX family.
- EROFS
- The socket inode would reside on a read-only file system.
- EFAULT
- my_addr points outside the user's accessible address space.
- ENAMETOOLONG
- my_addr is too long.
- ENOENT
- The file does not exist.
- ENOMEM
- Insufficient kernel memory was available.
- ENOTDIR
- A component of the path prefix is not a directory.
- EACCES
- Search permission is denied on a component of the path prefix.
- ELOOP
- Too many symbolic links were encountered in resolving my_addr.
BUGS
The transparent proxy options are not described.
CONFORMING TO
SVr4, 4.4BSD (the bind function first appeared in BSD 4.2). SVr4 documents additional EADDRNOTAVAIL, EADDRINUSE, and ENOSR general error conditions, and additional EIO and EISDIR Unix-domain error conditions.
NOTE
The third argument of bind is in reality an int (and this is what BSD 4.* and libc4 and libc5 have). Some POSIX confusion resulted in the present socklen_t. See also accept(2).
SEE ALSO
accept(2), connect(2), listen(2), socket(2), getsockname(2), ip(7), socket(7)