获取服务器的IP 0.0.0.0:0从的getaddrinfo() [英] Getting server ip 0.0.0.0:0 from getaddrinfo()

查看:1500
本文介绍了获取服务器的IP 0.0.0.0:0从的getaddrinfo()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面Beej对NP指南。

我做了一些修改,我试图通过的getaddrinfo()来获取我的服务器程序的IP。结果
(原可以在这里找到 http://beej.us/guide /bgnet/output/html/singlepage/bgnet.html#simpleserver

下面是我已经改变了部分/补充说。

  IF((RV =的getaddrinfo(NULL,0,和放大器;提示,&安培; servinfo))!= 0){// 0随机端口?
    fprintf中(标准错误的getaddrinfo:%S \\ n,gai_strerror(RV));
    返回1;
 }// ...一些code发出...// freeaddrinfo(servinfo); //我还需要它!
的printf(IP:%S \\的NPort数:%d \\ n,
    inet_ntop(AF_INET,及((结构SOCKADDR_IN *)P-> ai_addr) - GT; sin_addr,属于IP4,INET_ADDRSTRLEN)
    还有ntohs(((结构SOCKADDR_IN *)P-> ai_addr) - GT; sin_port)
);

问题是,我得到的结果。

  IP:0.0.0.0
端口:0

Q1:我从一对夫妇的网站说,设置0的端口告诉你想要的下一个可用端口操作系统,实际上不是0被读取,这真的

Q2:我也读到的gethostbyname(的gethostname(...))可以给你的机器的IP,但Beej说,这些被取代的getaddrinfo()。所以,我应该使用getaddrinfo?或的gethostbyname?

Q3:还有什么我做错了。


解决方案

  

Q1:我从一对夫妇的网站说,设置0的端口告诉你想要的下一个可用端口操作系统,实际上不是0被读取,这真的


是的,但你已经使用后才绑定()的地址连接到一个真正的插座。在这一点上,使用 getsockname()来获得从插座中绑定的地址;该端口将成为其中的一部分。


  

Q2:我也读到的gethostbyname(的gethostname(...))可以给你的机器的IP,但Beej说,这些被取代的getaddrinfo()。所以,我应该使用getaddrinfo?或的gethostbyname?


使用的getaddrinfo();它所有的的gethostbyname()做多,而且界面吸少了很多。 (例如,它通常是线程安全的。)


  

Q3:还有什么我做错了。


有服务器的IP地址没有很好的定义的概念。服务器可以有很多,由于像多块网卡的事情(更常见比桌面系统服务器),而受到外界知道它的人可能不是其中之一(感谢NAT防火墙)。偶尔,你可以准确地知道消息是从客户端连接之前出来的话 - 最常见的选择是要知道客户端将在本地主机 - 这是部分您在设置绑定()但这是不可多得的资料。您的可以的找到客户端的地址,一旦连接使用 getpeername()发生,但NAT仍然可能使该功能无用。这样的地址在部署过程中应用程序的配置文件通常设置。

如果您只需要进行日志记录的信息,请便。但要注意使用它为别的,因为它实际上并没有告诉你这么多。

I'm following Beej's guide on NP.

I did a few modifications and am trying to obtain the IP of my server program through getaddrinfo().
(original can be found here http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver)

Below is the parts I've changed/added.

if ((rv = getaddrinfo(NULL, "0", &hints, &servinfo)) != 0) { //0 for random port?
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
 }

//... some code emitted ...

//freeaddrinfo(servinfo); //I still need it!


printf("ip: %s\nport: %d\n",
    inet_ntop(AF_INET, &((struct sockaddr_in *)p->ai_addr)->sin_addr, ip4, INET_ADDRSTRLEN),
    ntohs(((struct sockaddr_in *)p->ai_addr)->sin_port)
);

The problem is that I get results

ip: 0.0.0.0  
port: 0  

Q1:I've read from a couple of websites saying that setting "0" for the port tells the OS that you want the next available port, not actually 0. Is this true?

Q2:I've also read that gethostbyname(gethostname(...)) can give you the machine's ip, but Beej said that these are superseded by getaddrinfo(). So, am I supposed to use getaddrinfo? Or gethostbyname?

Q3:Is there anything else I'm doing wrong?

解决方案

Q1:I've read from a couple of websites saying that setting "0" for the port tells the OS that you want the next available port, not actually 0. Is this true?

Yes, but only after you have used bind() to attach the address to a real socket. At that point, use getsockname() to get the bound address from the socket; the port will be part of that.

Q2:I've also read that gethostbyname(gethostname(...)) can give you the machine's ip, but Beej said that these are superseded by getaddrinfo(). So, am I supposed to use getaddrinfo? Or gethostbyname?

Use getaddrinfo(); it does all that gethostbyname() did and more, and the interface sucks a lot less. (For example, it's typically thread-safe.)

Q3:Is there anything else I'm doing wrong?

There's no good defined concept of the server's IP address. Servers can have many due to things like multiple network cards (much more common for servers than desktop systems), and the one that the outside world knows it by might not be one of them (thanks to NAT firewalls). Occasionally, you can know exactly where the messages are coming from before the client connects — the most common option is to know that the client will be on localhost — which is part of the information you set during bind() but that's rare. You can find the address of the client once the connection has happened by using getpeername() but NAT might still make that functionally useless. Such addresses are typically set in the app's config file during deployment.

If you only need the info for logging purposes, go right ahead. But beware of using it for anything else, as it doesn't actually tell you that much.

这篇关于获取服务器的IP 0.0.0.0:0从的getaddrinfo()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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