分类: LINUX
2011-05-16 14:16:27
Linux Programer’s Manual - BIND(2)
调用创建套接口, 分配了一个描述符(地址族), 但并没有为这个描述符指定具体的地址. bind() 将把由addr指定的地址指派到由sockfd引用的文件描述符上. addrlen以字节为单位, 表示地址结构addr的长度. 通常, 这个操作也被称为”为一个套接口命名”.
It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may receive connections (see ).不同的地址族之间绑定描述符的规则不同, 具体请参阅手册7中的详尽信息. AF_INET参见, AF_INET6参见, AF_UNIX参见, AF_APPLETALK参见, AF_PACKET参见, AF_X25参见, AF_NETLINK参见.
The actual structure passed for the addr argument will depend on the address family. The sockaddr structure is defined as something like:addr参数传递的实际结构将取决于具体的地址族, 参数sockaddr的结构定义如下
struct sockaddr { sa_family_t sa_family; char sa_data[14]; } |
该结构的唯一目的就是通过addr结构指针的转换, 从而避免编译器警告.
RETURN VALUEEACCES The address is protected, and the user is not the superuser.
EACCES 地址被保护, 用户无超级用户权限.
EADDRINUSE The given address is already in use.
EADDRINUSE 给定的地址正在使用
EBADF sockfd is not a valid descriptor.
EBADF sockfd参数并非一个可用描述符
EINVAL The socket is already bound to an address.
EINVAL 套接口已经和其他地址绑定
ENOTSOCK sockfd is a descriptor for a file, not 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 .)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指针超出了用户地址空间
EINVAL The addrlen is wrong, or the socket was not in the AF_UNIX family.
EINVAL addrlen错误或套接口并非AF_UNIX族套接口
ELOOP Too many symbolic links were encountered in resolving addr.
ELOOP 在解析addr时遇到太多符号链接
ENAMETOOLONG addr is too long.
ENAMETOOLONG addr太长
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 file system.
EROFS 套接口索引节点属于一个只读文件系统中
CONFORMING TO POSIX.1-2001和Linux不要求包含
可以在中找到因特网域套接口关于bind()的示例.
The following example shows how to bind a stream socket in the UNIX (AF_UNIX) domain, and accept connections:下面例子说明了在Unix域协议(AF_UNIX)如何绑定套接口并接受连接.
#include #include #include #include #include
#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; sockaddr_un myaddr; sockaddr_un peeraddr; socklen_t peer_addr_size;
sfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sfd == -1) handle_error("socket"); memset(&myaddr, 0, sizeof(struct sockaddr_un)); /* Clear structure */ myaddr.sun_family = AF_UNIX; strncpy(myaddr.sun_path, MY_SOCK_PATH, sizeof(myaddr.sun_path) - 1); if (bind(sfd, (struct sockaddr *) &myaddr, 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 *) &peeraddr, &peer_addr_size); if (cfd == -1) handle_error("accept"); /* 处理连接的代码 */ } |
本页是Linux man-pages项目3.32发布版的一部分. 本项目的描述和相关bug信息可在中找到
Scorpio 2011-5-16 14:23:16