BSD 套接字连接时参数无效

发布于 2024-11-24 03:20:10 字数 1636 浏览 4 评论 0原文

当我尝试将客户端连接到服务器时,我不断收到无效参数错误。网上有几个线程说当 addrlen 不正确时可能会发生这种情况,但我尝试在计算长度后将其更改为文字值,但这不起作用。我也尝试过 strlen() 但没有成功。无论如何,相关代码-

服务器-

struct sockaddr name;
int main(int agrc, char** argv) {

    int sock, new_sd, adrlen;   //sock is this socket, new_sd is connection socket

    name.sa_family = AF_INET;
    strcpy(name.sa_data, "127.0.0.1");
    adrlen = strlen(name.sa_data) + sizeof(name.sa_family);

    //make socket
    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock < 0) {
        printf("\nBind error %m", errno);
        exit(1);
    }  

    //unlink and bind
    unlink("127.0.0.1");
    if(bind (sock, &name, adrlen) < 0)
        printf("\nBind error %m", errno);

    //listen
    if(listen(sock, 5) < 0)
        printf("\nListen error %m", errno);

    //accept
    new_sd = accept(sock, &name, (socklen_t*)&adrlen);
    if( new_sd < 0) {
        printf("\nAccept error %m", errno);
        exit(1);
    }

客户端-

int main(int agrc, char** argv) {

    int sock, new_sd, adrlen;

    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock < 0) {
        printf("\nserver socket failure %m", errno);
        exit(1);
    }

    //stuff for server socket
    name.sa_family = AF_INET;
    strcpy(name.sa_data, "127.0.0.1");
    adrlen = strlen(name.sa_data) + sizeof(name.sa_family);

    cout<<"\nadrlen: "<<adrlen<<"\n";
    if(connect(sock, &name, adrlen) < 0) {
        printf("\nclient connection failure %m", errno);
        exit(1);
    }

我没有看到任何可能错误的地方,但我想我可能只是忽略了某些东西或没有意识到某些东西。任何帮助表示赞赏。

I keep getting an invalid argument error when I try to connect the client to the server. A couple threads online said this can happen when addrlen is not right, but I tried changing it to a literal value after counting the length and that did not work. I also tried just strlen() with no luck. Anyways, relevant code -

server -

struct sockaddr name;
int main(int agrc, char** argv) {

    int sock, new_sd, adrlen;   //sock is this socket, new_sd is connection socket

    name.sa_family = AF_INET;
    strcpy(name.sa_data, "127.0.0.1");
    adrlen = strlen(name.sa_data) + sizeof(name.sa_family);

    //make socket
    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock < 0) {
        printf("\nBind error %m", errno);
        exit(1);
    }  

    //unlink and bind
    unlink("127.0.0.1");
    if(bind (sock, &name, adrlen) < 0)
        printf("\nBind error %m", errno);

    //listen
    if(listen(sock, 5) < 0)
        printf("\nListen error %m", errno);

    //accept
    new_sd = accept(sock, &name, (socklen_t*)&adrlen);
    if( new_sd < 0) {
        printf("\nAccept error %m", errno);
        exit(1);
    }

client -

int main(int agrc, char** argv) {

    int sock, new_sd, adrlen;

    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock < 0) {
        printf("\nserver socket failure %m", errno);
        exit(1);
    }

    //stuff for server socket
    name.sa_family = AF_INET;
    strcpy(name.sa_data, "127.0.0.1");
    adrlen = strlen(name.sa_data) + sizeof(name.sa_family);

    cout<<"\nadrlen: "<<adrlen<<"\n";
    if(connect(sock, &name, adrlen) < 0) {
        printf("\nclient connection failure %m", errno);
        exit(1);
    }

I don't see anything that could be wrong, but I guess I might just be overlooking something or unaware of something. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春庭雪 2024-12-01 03:20:10
strcpy(name.sa_data, "127.0.0.1");

真的吗?地址应该是 32 位 IPv4 地址,而不是字符串。

这是 AF_INET 的结构(来自此处):

// IPv4 AF_INET sockets:

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};
strcpy(name.sa_data, "127.0.0.1");

Really? The address should be the 32 bit IPv4 address, not a string.

This is the structure for AF_INET (from here):

// IPv4 AF_INET sockets:

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};
忘你却要生生世世 2024-12-01 03:20:10

那么你的大问题是完全误解了 sockaddr 结构!

首先使用sockaddr_in

然后,您将编写类似于以下的代码:

sockaddr_in sai;
sai.sin_family = AF_INET;
sai.sin_port   = htons( 12345 );  /// Or whatever port you wish to use.
inet_aton("127.0.0.1", &sai.sin_addr.s_addr);

注意:htons 将短值从主机格式转换为网络格式。主机可以是大端或小端。网络是大端字节序。

Well your big problem is completely misunderstanding the sockaddr struct!

Firstly use sockaddr_in.

You would then write code more like the following:

sockaddr_in sai;
sai.sin_family = AF_INET;
sai.sin_port   = htons( 12345 );  /// Or whatever port you wish to use.
inet_aton("127.0.0.1", &sai.sin_addr.s_addr);

Note: htons converts a short value from host to network format. Host could be big or little endian. Network is big endian.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文