bind 中文man頁(yè)面
NAME 名稱(chēng)
bind - 將一個(gè)名字和一個(gè)套接字綁定到一起(賦一個(gè)名字給一個(gè)套接字)
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 的長(zhǎng)度為 addrlen (字節(jié)).傳統(tǒng)的叫法是給一個(gè)套接字分配一個(gè)名字. 當(dāng)使用 socket(2), 函數(shù)創(chuàng)建一個(gè)套接字時(shí),它存在于一個(gè)地址空間(地址族), 但還沒(méi)有給它分配一個(gè)名字
一般來(lái)說(shuō)在使用 SOCK_STREAM 套接字建立連接之前總要使用 bind 為其分配一個(gè)本地地址.參見(jiàn) accept(2)).
NOTES 注意
這條規(guī)則用于給每個(gè)地址族綁定不同的名稱(chēng).更多細(xì)節(jié)請(qǐng)參考手冊(cè)頁(yè)第7冊(cè)(man7). 對(duì)于 AF_INET 參見(jiàn) ip(7), 對(duì)于 AF_UNIX 參見(jiàn) unix(7), 對(duì)于 AF_APPLETALK 參見(jiàn) ddp(7), 對(duì)于 AF_PACKET 參見(jiàn) packet(7), 對(duì)于r AF_X25 參見(jiàn) x25(7) 對(duì)于 AF_NETLINK 參見(jiàn) netlink(7).
RETURN VALUE 返回值
函數(shù)執(zhí)行成功返回0,否則返回-1, 并設(shè)置錯(cuò)誤代碼.
ERRORS 錯(cuò)誤
- EBADF
- sockfd 不是一個(gè)合法套接字描述符.
- EINVAL
- 套接字已經(jīng)綁定到一個(gè)地址.這一條在以后會(huì)有所改變: 具體參見(jiàn) linux/unix/sock.c
- EACCES
- 地址受保護(hù),用戶不是系統(tǒng)管理員.
- ENOTSOCK
- 參數(shù)是文件描述符,不是一個(gè)套接字.
下列錯(cuò)誤適用于UNIX域 (AF_UNIX) 套接字.
- EINVAL
- 地址長(zhǎng)度 addrlen 錯(cuò)誤,或者套接字不在 AF_UNIX 族.
- EROFS
- 套接字節(jié)點(diǎn)位于只讀文件系統(tǒng).
- EFAULT
- my_addr 指向用戶無(wú)權(quán)訪問(wèn)的地址空間.
- ENAMETOOLONG
- my_addr 長(zhǎng)度超范圍.
- ENOENT
- 文件不存在.
- ENOMEM
- 內(nèi)核存儲(chǔ)空間不足.
- ENOTDIR
- 指定路徑不是一個(gè)目錄.
- EACCES
- 指定路徑拒絕訪問(wèn).
- ELOOP
- 在解析 my_addr 時(shí)發(fā)現(xiàn)過(guò)多符號(hào)連接.
BUGS 勘誤
透明代理選項(xiàng)沒(méi)有描述.
CONFORMING TO 一致性
SVr4,4.4BSD(函數(shù) bind 首次出現(xiàn)于BSD 4.2)SVr4文檔增加了 EADDRNOTAVAIL, EADDRINUSE, 和 ENOSR 一般性錯(cuò)誤, 還增加了 EIO, EISDIR 和 EROFS Unix域錯(cuò)誤.
NOTE
函數(shù) bind 的第三個(gè)參數(shù)實(shí)際上是int類(lèi)型(BSD 4.*和libc4以及l(fā)ibc5都是這么做的). 不知為什么有的POSIX系統(tǒng)目前仍在使用socklen_t. 目前尚無(wú)統(tǒng)一標(biāo)準(zhǔn),不過(guò)glibc2兩者都使用.參見(jiàn) accept(2).
SEE ALSO 參見(jiàn)
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)