为什么'接受(2)`需要`sockaddr`长度作为一个单独的指针? [英] Why does `accept(2)` need the `sockaddr` length as a separate pointer?

查看:332
本文介绍了为什么'接受(2)`需要`sockaddr`长度作为一个单独的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力理解的原型接受(2)

我有接受连接,并返回一个简单的服务器的Hello world!\\ n来的客户。系统调用接受(2)需要一个指向结构sockaddr 和一个指向 socklen_t的来存储结构的长度。但的sockaddr 已经有一个叫场 sa_len ,这似乎是刚刚完成的。

另外,我有这个简单的服务器(OSX下的编译,希望Linux的太)打印出我自己的 socklen_t的传递给接受,然后价值 sa_len :它们是相同的,在这种情况下28,在OSX

编辑:一个多一点的测试后,似乎 sa_len 不一定一样存储在指针长度

为什么接受(2)所需要的长度作为一个单独的指针?


有关参考,我张贴下面的例子服务器。你可以编译:

 的gcc -Wall -Wextra的main.c

然后运行:

  ./ a.out的

在另一端,连接到它:

  127.0.0.1的telnet 3000


 的#include<&errno.h中GT;
#包括LT&; SYS / socket.h中>
#包括LT&;&unistd.h中GT;
#包括LT&;&netdb.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&; netinet / in.h中>
#包括LT&;&strings.h GT;//如果没有,如果传递的程序参数默认端口
#定义FTP_PORT_DEFAULT(3000)//连接数允许等待呼叫接受
的#define FTP_BACKLOG(5)INT主(INT交流,焦炭** AV)
{
    结构addrinfo中的提示; //提示用于获取地址信息
    结构addrinfo中的*赛; //服务器地址信息
    INT SS; //服务器套接字
    字符*口;    端口=(AC&GT = 2)? AV [1]:FTP_PORT_DEFAULT;
    bzero(安培;提示,sizeof的(提示));    使用AF_INIT6代替PF_INET6 //
    // http://stackoverflow.com/questions/6729366/
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;    //用于与`bind`使用
    hints.ai_flags = AI_PASSIVE;    //传递`NULL`作为主机名告诉`getaddrinfo`使用
    //任何可用的本地IP地址
    如果(的getaddrinfo(NULL,端口&放大器;提示,&放大器;!SAI)= 0)
        返回(-1);    如果((SS =
         插座(SAI-> ai_family,SAI-> ai_socktype,SAI-> ai_protocol))== -1)
        返回(-1);    如果(绑定(SS,SAI-> ai_addr,SAI-> ai_addrlen)== -1 ||
        听(SS,FTP_BACKLOG)== -1)
    {
        关闭(SS);
        返回(-1);
    }    而(1)
    {
        INT CS;
        结构sockaddr csockaddr;
        无符号csockaddr_len;        bzero(安培; csockaddr,sizeof的(csockaddr));
        CS =接受(SS,和放大器; csockaddr,&安培; csockaddr_len);
        //处理致命我不认为我可以从错误中恢复
        //比通过重新启动服务器的其他
        如果(CS == EBADF || CS == || ECONNABORTED CS == || ENOTSOCK
                CS == EOPNOTSUPP)
        {
            突破;
        }
        的printf(我的LEN%U /内部L​​EN%U \\ N,
                   csockaddr_len,csockaddr.sa_len);        如果(叉()== 0)
        {
            写(CS,的Hello world \\ n!,13);
            关闭(CS);
        }
        打破;
    }
    关闭(SS);    返回0;
}


解决方案

sa_len 是不可移植。据只加于1988年BSD ,4.3BSD几年后,但它不是在 POSIX ,它不是在Linux中。

I am struggling to understand the prototype of accept(2).

I have a simple server that accepts connections and returns "Hello world!\n" to the clients. The system call accept(2) takes a pointer to struct sockaddr and a pointer to an socklen_t to store the length of the struct. But sockaddr already has a field called sa_len, which seems to be done just for that.

In addition, I have this simple server (compiles under OSX, hopefully Linux too) that prints out my own socklen_t passed to accept and then the value of sa_len: they are the same, 28 in this case, on OSX.

EDIT: After a bit more testing, it seems that sa_len is not necessarily the same as the length stored in the pointer.

Why does accept(2) need the length as a separate pointer?


For reference, I'm posting the example server below. You can compile with:

gcc -Wall -Wextra main.c

And then run:

./a.out

In another terminal, connect to it:

telnet 127.0.0.1 3000


#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <netinet/in.h>
#include <strings.h>

// default port if none if passed in the program arguments
#define FTP_PORT_DEFAULT ("3000")

// number of connections allow to wait for a call to accept
#define FTP_BACKLOG (5)

int main(int ac, char **av)
{
    struct addrinfo     hints; // hints used to get address information
    struct addrinfo     *sai; // server address information
    int                 ss; // server socket
    char            *port;

    port = (ac >= 2) ? av[1] : FTP_PORT_DEFAULT;
    bzero(&hints, sizeof(hints));

    // using AF_INIT6 instead of PF_INET6
    // http://stackoverflow.com/questions/6729366/
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;

    // intended for use with `bind`
    hints.ai_flags = AI_PASSIVE;

    // passing `NULL` as the hostname tells `getaddrinfo` to use
    // any available local IP address
    if (getaddrinfo(NULL, port, &hints, &sai) != 0)
        return (-1);

    if ((ss =
         socket(sai->ai_family, sai->ai_socktype, sai->ai_protocol))== -1)
        return (-1);

    if (bind(ss, sai->ai_addr, sai->ai_addrlen) == -1 ||
        listen(ss, FTP_BACKLOG) == -1)
    {
        close(ss);
        return (-1);
    }

    while (1)
    {
        int cs;
        struct sockaddr csockaddr;
        unsigned csockaddr_len;

        bzero(&csockaddr, sizeof(csockaddr));
        cs = accept(ss, &csockaddr, &csockaddr_len);
        // handle "fatal" errors that I don't think I can recover from
        // other than by relaunching the server
        if (cs == EBADF || cs == ECONNABORTED || cs == ENOTSOCK ||
                cs == EOPNOTSUPP)
        {
            break ;
        }
        printf("my len %u / internal len %u\n",
                   csockaddr_len, csockaddr.sa_len);

        if (fork() == 0)
        {
            write(cs, "Hello world!\n", 13);
            close(cs);
        }
        break;
    }
    close(ss);

    return 0;
}

解决方案

sa_len is not portable. It was added to BSD only in 1988, a few years after 4.3BSD, but it's not in POSIX and it's not in Linux.

这篇关于为什么'接受(2)`需要`sockaddr`长度作为一个单独的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆