getsockname总是返回0.0.0.0? [英] getsockname always returning 0.0.0.0?

查看:1888
本文介绍了getsockname总是返回0.0.0.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code。这是一样的,从这个类似的问题在code: HTTP: //monkey.org/freebsd/archive/freebsd-stable/200401/msg00032.html 。当我运行它,我总是得到的输出:

监听0.0.0.0:54493什么的。显然,端口变化,但我不知道为什么我总是收到的IP地址0.0.0.0。我缺少的东西吗?

 的#include<&stdio.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; netinet / in.h中>
#包括LT&; ARPA / inet.h>诠释的main()
{
    INT袜子;
    INT LEN = sizeof的(结构sockaddr);
    结构SOCKADDR_IN地址,foo的;    如果((袜子=插座(AF_INET,SOCK_STREAM,0))小于0)
    {
        出口(0);
    }    memset的(放,地址,0,sizeof的(结构SOCKADDR_IN));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(0);    如果(绑定(袜子,(结构sockaddr *)及地址,的sizeof(结构SOCKADDR_IN))小于0)
    {
        PERROR(绑定);
        出口(0);
    }    如果(听(袜子,5)℃,)
    {
         PERROR(听);
         出口(0);
    }    getsockname(袜子,(结构sockaddr *)及富,和放大器; LEN);
    fprintf中(标准错误,听在%s:%d个\\ N,INET_NTOA(foo.sin_addr)
            还有ntohs(foo.sin_port));    返回0;
}


解决方案

您指定 INADDR_ANY ,而不是特定的IP地址,所以将其绑定到通配符(所有接口) 0.0.0.0 。所以,当你调用 getsockname()这就是你会得到什么。

如果您指定 0.0.0.0 作为IP地址,而不是 INADDR_ANY 你会得到相同的行为;你会绑定到计算机上的所有网络接口。

举例来说,假设你只有用IP 192.168.1.12 分配给它的一个网络接口。您还可以在默认情况下环回 - 127.0.0.1

使用 0.0.0.0 INADDR_ANY 意味着你将被绑定到这两个地址,而不是特定的一。您将可以通过任何IP连接到你的过程。

如果你要绑定到特定的IP,而不是 INADDR_ANY ,你的进程将只侦听该IP和那你将获得与<$ C特定的IP $ C> getsockname()。

Here is the code. It is the same as the code from this similar question: http://monkey.org/freebsd/archive/freebsd-stable/200401/msg00032.html. When I run it I always get the output:

listening on 0.0.0.0:54493 or something. Obviously the port changes, but I have no idea why I keep getting an IP address of 0.0.0.0. Am I missing something?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    int sock;
    int len = sizeof(struct sockaddr);
    struct sockaddr_in addr, foo;

    if((sock=socket(AF_INET, SOCK_STREAM, 0))<0)
    {
        exit(0);
    }

    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(0);

    if(bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))<0)
    {
        perror("bind");
        exit(0);
    }

    if(listen(sock, 5)<0)
    {
         perror("listen");
         exit(0);
    }

    getsockname(sock, (struct sockaddr *) &foo, &len);
    fprintf(stderr, "listening on %s:%d\n", inet_ntoa(foo.sin_addr), 
            ntohs(foo.sin_port));

    return 0;
}

解决方案

You specify INADDR_ANY rather than a specific IP address, so it binds to the wildcard (all interfaces) 0.0.0.0. So, when you call getsockname() that's what you get back.

If you specified 0.0.0.0 as the IP address rather than INADDR_ANY you would get the same behavior; you will bind to all network interfaces on the machine.

For example, lets say you only have one network interface with the IP 192.168.1.12 assigned to it. You also have the loopback by default - 127.0.0.1

Using 0.0.0.0 or INADDR_ANY means you'll be bound to both those addresses, rather than a specific one. You will be able to connect to to your process via either IP.

If you were to bind to a specific IP rather than INADDR_ANY, your process would only listen on that IP and you'd get back that specific IP with getsockname().

这篇关于getsockname总是返回0.0.0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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