Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111798
  • 博文数量: 19
  • 博客积分: 1716
  • 博客等级: 上尉
  • 技术积分: 275
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-25 14:03
文章分类

全部博文(19)

文章存档

2011年(8)

2010年(11)

我的朋友

分类: LINUX

2011-05-16 14:16:27

Linux Programer’s Manual - BIND(2) 
NAME        
       bind - bind a name to a socket 
SYNOPSIS
       #include           /* See NOTES */
       #include
       int bind(int sockfd, const struct sockaddr *addr,
                socklen_t addrlen);
DESCRIPTION
       When a socket is created with , it exists in a name space (address family) but has no address assigned to it.  bind() assigns the address specified to by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr.  Traditionally, this operation is called "assigning a name to a socket".

       调用创建套接口, 分配了一个描述符(地址族), 但并没有为这个描述符指定具体的地址. bind() 将把由addr指定的地址指派到由sockfd引用的文件描述符上. addrlen以字节为单位, 表示地址结构addr的长度. 通常, 这个操作也被称为为一个套接口命名”.

         It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may receive connections (see ).
         在使用SOCK_STREAM套接口接收连接之前, 通常需要使用bind()指派一个本地地址(参阅).
         The rules used in name binding vary between address families.  Consult the manual entries in Section 7 for detailed information.  For AF_INET see , for AF_INET6 see , for AF_UNIX see , for AF_APPLETALK see , for AF_PACKET see , for AF_X25 see and for AF_NETLINK see .

       不同的地址族之间绑定描述符的规则不同, 具体请参阅手册7中的详尽信息. AF_INET参见, AF_INET6参见, AF_UNIX参见, AF_APPLETALK参见, AF_PACKET参见, AF_X25参见, AF_NETLINK参见.

       The actual structure passed for the addr argument will depend on the address family.  The sockaddr structure is defined as something like:

       addr参数传递的实际结构将取决于具体的地址族, 参数sockaddr的结构定义如下

struct sockaddr {

    sa_family_t sa_family;

    char        sa_data[14];

}

       The only purpose of this structure is to cast the structure pointer passed in addr in order to avoid compiler warnings.  See EXAMPLE below.

       该结构的唯一目的就是通过addr结构指针的转换, 从而避免编译器警告.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
ERRORS

       EACCES The address is protected, and the user is not the superuser.

       EACCES 地址被保护, 用户无超级用户权限.

       EADDRINUSE The given address is already in use.

       EADDRINUSE 给定的地址正在使用

       EBADF sockfd is not a valid descriptor.

       EBADF sockfd参数并非一个可用描述符

       EINVAL The socket is already bound to an address.

       EINVAL 套接口已经和其他地址绑定

       ENOTSOCK sockfd is a descriptor for a file, not a socket.

       ENOTSOCK sockfd是一个文件描述符, 并非一个套接口

 

       The following errors are specific to UNIX domain (AF_UNIX) sockets:

       下面的错误在Unix域协议(AF_UNIX)中使用

       EACCES Search permission is denied on a component of the path prefix.  (See also .)

       EACCES 在路径前缀组件中搜索权限被拒绝, 参见

       EADDRNOTAVAIL A nonexistent interface was requested or the requested address was not local.

       EADDRNOTAVAIL 请求一个不存在的接口, 或者请求的地址不是本地的.

       EFAULT addr points outside the user's accessible address space.

       EFAULT addr指针超出了用户地址空间

       EINVAL The addrlen is wrong, or the socket was not in the AF_UNIX family.

       EINVAL addrlen错误或套接口并非AF_UNIX族套接口

       ELOOP Too many symbolic links were encountered in resolving addr.

       ELOOP 在解析addr时遇到太多符号链接

       ENAMETOOLONG addr is too long.

       ENAMETOOLONG addr太长

       ENOENT The file does not exist.

       ENOENT 文件不存在

       ENOMEM Insufficient kernel memory was available.

       ENOMEM 内核空间不足

       ENOTDIR A component of the path prefix is not a directory.

       ENOTDIR 路径前缀组件不是目录

       EROFS  The socket inode would reside on a read-only file system.

       EROFS 套接口索引节点属于一个只读文件系统中

CONFORMING TO
       SVr4, 4.4BSD, POSIX.1-2001 (bind() first appeared in 4.2BSD).
NOTES
       POSIX.1-2001 does not require the inclusion of , and this header file is not required on Linux.  However, some historical (BSD) implementations required this header file, and portable applications are probably wise to include it.

       POSIX.1-2001Linux不要求包含, 但一些历史版本的实现要求包含这个头文件. 一些便携应用中通常包含这个头文件.

         The third argument of bind() is in reality an int (and this is what 4.x BSD and libc4 and libc5 have).  Some POSIX confusion resulted in the present socklen_t, also used by glibc.  See also .
       bind()的第三个参数实际上是int类型(4.xBSD, libc4, libc5), 一些POSIX对此与socklen_t混淆了, glibc中仍然使用socklen_t, 参阅.
BUGS
       The transparent proxy options are not described.
       透明代理选项未被描述
EXAMPLE
       An example of the use of bind() with Internet domain sockets can be found in .

       可以在中找到因特网域套接口关于bind()的示例.

         The following example shows how to bind a stream socket in the UNIX (AF_UNIX) domain, and accept connections:

       下面例子说明了在Unix域协议(AF_UNIX)如何绑定套接口并接受连接.

#include 

#include 

#include 

#include 

#include 

 

#define MY_SOCK_PATH "/somepath"

#define LISTEN_BACKLOG 50

 

#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)

 

int main(int argc, char *argv[])

{

    int sfd, cfd;

    sockaddr_un myaddr;

    sockaddr_un peeraddr;

    socklen_t peer_addr_size;

 

    sfd = socket(AF_UNIX, SOCK_STREAM, 0);

    if (sfd == -1)

        handle_error("socket");

    memset(&myaddr, 0, sizeof(struct sockaddr_un));

    /* Clear structure */

    myaddr.sun_family = AF_UNIX;

    strncpy(myaddr.sun_path, MY_SOCK_PATH,

            sizeof(myaddr.sun_path) - 1);

    if (bind(sfd, (struct sockaddr *) &myaddr, sizeof(struct sockaddr_un)) == -1)

        handle_error("bind");

    if (listen(sfd, LISTEN_BACKLOG) == -1)

        handle_error("listen");

    /* Now we can accept incoming connections one at a time using accept(2) */

    peer_addr_size = sizeof(struct sockaddr_un);

    cfd = accept(sfd, (struct sockaddr *) &peeraddr, &peer_addr_size);

    if (cfd == -1)

        handle_error("accept");

    /* 处理连接的代码 */

}

SEE ALSO
       , , , , , , , , , , ,
COLOPHON
       This page is part of release 3.32 of the Linux man-pages project.  A description of the project, and information about reporting bugs, can be found at .

       本页是Linux man-pages项目3.32发布版的一部分. 本项目的描述和相关bug信息可在中找到

 

Scorpio 2011-5-16 14:23:16

阅读(2287) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~