本篇翻译的bind()函数,我参考的国外网站是:
bind
朋友们可以自由转载我对英文的中文翻译,但是对于“作者注:”的描述,转载时请注明出处和作者,否则视为侵权。
下面是翻译的正文,由于水平有限,有些翻译的不好,有些未能翻译出,恳请读者指出与见谅。
NAME
bind - bind a name to a socket
bind一个名字到一个套接字上。
SYNOPSIS
#include
int bind(int socket, const struct sockaddr *address,
socklen_t address_len);
DESCRIPTION
The bind() function assigns an address to an unnamed socket. Sockets created with socket() function are initially unnamed; they are identified only by their address family.
bind()函数将一个地址分配给一个未命名的套接字。使用socket()函数创建的那些套接字初始化是没有命名的,它们只有通过地址族才能被识别。
The function takes the following arguments:
函数的参数如下:
socket
Specifies the file descriptor of the socket to be bound.
socket参数:指定了需要绑定的套接字的文件描述符。
作者注:
socket参数是一个文件描述符,是socket()函数的返回值。
address
Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket.
Address参数:指向一个sockaddr结构体,这个结构体中包含着要绑定到套接字的地址。地址的长度和格式依赖于套接字支持的地址族。
作者注:
1.套接字作为系统的一个文件,进程要通过这个文件来通信,因此就要使用这个文件。要使用这个文件,得先要绑定这个文件,才能用这个文件。
2.struct sockaddr结构体如下,摘自
include/linux/socket.h
struct sockaddr
{
unsigned short sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
};
- 给sa_data赋值时,需要注意网络字节序的问题。
address_len
Specifies the length of the sockaddr structure pointed to by the address argument.
address_len参数:指定了sockaddr结构体的长度。参数address 指向了这个sockaddr结构体。
The socket in use may require the process to have appropriate privileges to use the bind() function.
被使用的套接字也许会要求进程有合适的权限来使用bind()函数。
RETURN VALUE
Upon successful completion, bind() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.
一旦bind()函数成功执行,函数会返回0.否则的话就返回-1,而且errno也被设置用来解释是什么错误。
ERRORS
The bind() function will fail if:
bind()函数会因为下列原因而失败:
[EADDRINUSE]
The specified address is already in use.
指定的address参数已经被使用了。
[EADDRNOTAVAIL]
The specified address is not available from the local machine.
指定的地址在本地机器上不能使用。
[EAFNOSUPPORT]
The specified address is not a valid address for the address family of the specified socket.
指定的地址对于指定的套接字的地址族来说,是一个无效的地址。
[EBADF]
The socket argument is not a valid file descriptor.
socket参数本身就不是一个有效的文件描述符。
[EFAULT}
The address argument can not be accessed.
无法访问address参数。
[EINVAL]
The socket is already bound to an address, and the protocol does not support binding to a new address; or the socket has been shut down.
参数socket已经跟另外的地址绑定了,而且协议不支持再绑定一个新的地址。或者参数socket已经被关闭了。
[ENOTSOCK]
The socket argument does not refer to a socket.
socket参数没有引用到一个套接字。
作者注:
意思是说,bind()函数的socket参数虽然有一个值,也就是一个文件描述符,但是此描述符对应的socket文件没有了。
[EOPNOTSUPP]
The socket type of the specified socket does not support binding to an address.
参数socket的类型不支持绑定到一个地址。
作者注:
bind()函数一定会失败的原因,无外乎就是地址不对了,地址错了,地址不能用了,socket文件描述符不能用了。
If the address family of the socket is AF_UNIX, then bind() will fail if:
如果套接字的地址族是AF_UNIX,那么bind()函数会由于以下原因失败:
[EACCES]
A component of the path prefix denies search permission, or the requested name requires writing in a directory with a mode that denies write permission.
路径的前缀部分拒绝了搜索权限,或者被要求的名字要求以决绝写的模式写进一个目录。
作者注:这里不太好翻译。
[EDESTADDRREQ] or [EISDIR]
The address argument is a null pointer.
address参数是一个空指针。
[EIO]
An I/O error occurred.
一个I/O错误发生了。
[ELOOP]
Too many symbolic links were encountered in translating the pathname in address.
作者觉得不太好翻译,大概意思是说在翻译address中的路径名时,遇到了太多的象征性的链接。
[ENAMETOOLONG]
A component of a pathname exceeded {NAME_MAX} characters, or an entire pathname exceeded {PATH_MAX} characters.
一个路径名的一部分超过了{NAME_MAX}允许的最大长度,或者一个完整的超过了{PATH_MAX}允许的最大长度。
[ENOENT]
A component of the pathname does not name an existing file or the pathname is an empty string.
路径名的部分没有命名一个已存在的文件,或者路径名是一个空的字符串。
[ENOTDIR]
A component of the path prefix of the pathname in address is not a directory.
地址中的路径名的路径前缀部分不是一个目录。
[EROFS]
The name would reside on a read-only filesystem.
名字也许驻留在一个只读的文件系统中。
The bind() function may fail if:
bind()函数也许会由于以下原因失败:
[EACCES]
The specified address is protected and the current user does not have permission to bind to it.
指定的地址被保护了,当前用户没有权限来绑定。
[EINVAL]
The address_len argument is not a valid length for the address family.
address_len参数的长度对地址族来说,不是一个有效长度。
[EISCONN]
The socket is already connected.
套接字已经连接了。
[ENAMETOOLONG]
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.
链接的路径名解析产生了一个中间结果,这个结果的长度超过了{PATH_MAX}最大值。
[ENOBUFS]
Insufficient resources were available to complete the call.
没有足够的资源可用来完成调用。
[ENOSR]
There were insufficient STREAMS resources for the operation to complete.
没有足够的STREAMS资源来让操作完成。
APPLICATION USAGE
An application program can retrieve the assigned socket name with the getsockname() function.
一个应用程序可以通过getsockname()函数来获取绑定的套接字名。