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