.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) .\" Portions extracted from /usr/include/sys/socket.h, which does not have .\" any authorship information in it. It is probably available under the GPL. .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page: .\" .\" Copyright (c) 1983 The Regents of the University of California. .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond .\" Modified 1998 by Andi Kleen .TH BIND 2 "3 Oct 1998" "Linux 2.2" "Linux Programmer's Manual" .SH NAME 名稱 bind \- 將一個名字和一個套接字繫結到一起(賦一個名字給一個套接字) .SH SYNOPSIS 概述 .B #include .br .B #include .sp .BI "int bind(int " sockfd ", struct sockaddr *" my_addr ", socklen_t " addrlen ); .SH DESCRIPTION 描述 .B bind 為套接字 .I sockfd 指定本地地址 .IR my_addr . .I my_addr 的長度為 .I addrlen (位元組).傳統的叫法是給一個套接字分配一個名字. 當使用 .BR socket (2), 函式建立一個套接字時,它存在於一個地址空間(地址族), 但還沒有給它分配一個名字 .PP 一般來說在使用 .B SOCK_STREAM 套接字建立連線之前總要使用 .B bind 為其分配一個本地地址.參見 .BR accept (2)). .SH NOTES 注意 這條規則用於給每個地址族繫結不同的名稱.更多細節請參 考手冊頁第7冊(man7). 對於 .B AF_INET 參見 .BR ip (7), 對於 .B AF_UNIX 參見 .BR unix (7), 對於 .B AF_APPLETALK 參見 .BR ddp (7), 對於 .B AF_PACKET 參見 .BR packet (7), 對於r .B AF_X25 參見 .BR x25 (7) 對於 .B AF_NETLINK 參見 .BR netlink (7). .SH "RETURN VALUE" "返回值" 函式執行成功返回0,否則返回\-1, 並設定錯誤程式碼. .SH ERRORS 錯誤 .TP .B EBADF .I sockfd 不是一個合法套接字描述符. .TP .B EINVAL 套接字已經繫結到一個地址.這一條在以後會有所改變: 具體參見 .I linux/unix/sock.c .TP .B EACCES 地址受保護,使用者不是系統管理員. .TP .B ENOTSOCK 引數是檔案描述符,不是一個套接字. .PP 下列錯誤適用於UNIX域 .RB ( AF_UNIX ) 套接字. .TP .B EINVAL 地址長度 .I addrlen 錯誤,或者套接字不在 .B AF_UNIX 族. .TP .B EROFS 套接位元組點位於只讀檔案系統. .TP .B EFAULT .I my_addr 指向使用者無權訪問的地址空間. .TP .B ENAMETOOLONG .I my_addr 長度超範圍. .TP .B ENOENT 檔案不存在. .TP .B ENOMEM 核心儲存空間不足. .TP .B ENOTDIR 指定路徑不是一個目錄. .TP .B EACCES 指定路徑拒絕訪問. .TP .B ELOOP 在解析 .IR my_addr 時發現過多符號連線. .SH BUGS 勘誤 透明代理選項沒有描述. .SH CONFORMING TO 一致性 SVr4,4.4BSD(函式 .B bind 首次出現於BSD 4.2)SVr4文件增加了 .BR EADDRNOTAVAIL , .BR EADDRINUSE , 和 .B ENOSR 一般性錯誤, 還增加了 .BR EIO , .B EISDIR 和 .B EROFS Unix域錯誤. .SH NOTE 函式 .B bind 的第三個引數實際上是int型別(BSD 4.*和libc4以及libc5都是這麼做的). 不知為什麼有的POSIX系統目前仍在使用socklen_t. 目前尚無統一標準,不過glibc2兩者都使用.參見 .BR accept (2). .SH "SEE ALSO" "參見" .BR accept (2), .BR connect (2), .BR listen (2), .BR socket (2), .BR getsockname (2), .BR ip (7), .BR socket (7) .SH "跋" .br 本頁面中文版由中文 man 手冊頁計劃提供。 .br 中文 man 手冊頁計劃:\fBhttps://github.com/man-pages-zh/manpages-zh\fR