Socket编程(C语言实现):bind()函数英文翻译

本篇翻译的bind()函数,我参考的国外网站是:

bind

朋友们可以自由转载我对英文的中文翻译,但是对于“作者注:”的描述,转载时请注明出处和作者,否则视为侵权。

下面是翻译的正文,由于水平有限,有些翻译的不好,有些未能翻译出,恳请读者指出与见谅。

NAME

bind - bind a name to a socket

bind一个名字到一个套接字上。

SYNOPSIS

#include <sys/socket.h>

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 */

};
  1. 给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()函数来获取绑定的套接字名。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容

  • 最近开始研究使用Socket API来网络编程,想着把自己的感想、感悟写下来。我发现在编程之外还有不少概念性的东西...
    胖一一阅读 637评论 0 4
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,332评论 0 10
  • 网络编程 一.楔子 你现在已经学会了写python代码,假如你写了两个python文件a.py和b.py,分别去运...
    go以恒阅读 2,016评论 0 6
  • 一场秋雨一场寒,周六的雨,到了周日似乎还没有停歇的趋势,而身边的温度相较于前天已下降了一大截,清晨,显得格外冷。 ...
    壶中汀阅读 136评论 3 2
  • 经历过南方和北方的冬天,各有所爱,我喜欢南方的冬天。 虽然漫天的白雪,一望无际白茫茫一片,确实很冬天。 可是我,对...
    常寻lalala阅读 790评论 0 0