有没有让我的电脑的局域网IP地址的POSIX兼容的方式是什么? [英] Is there a POSIX-compliant way of getting local network IP address of my computer?

查看:82
本文介绍了有没有让我的电脑的局域网IP地址的POSIX兼容的方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了一百万次以这种或那种形式,但毫无疑问给了我这样做的符合POSIX标准的方式。

I know this question has been asked a million times in one form or another, but no question gave me a POSIX-compliant way of doing it.

我需要得到无是 getifaddrs 或访问接口的任何其他非POSIX兼容的方式在我的电脑的本地网络地址的。这可能吗?

I need to get the local network address of my computer without using getifaddrs or any other non POSIX-compliant way of accessing interfaces. Is that possible ?

推荐答案

有这样的事情没有直接的支持,但你可以使用UDP套接字( SOCK_DGRAM )以连接到已知遥远的地址。由于UDP套接字是无状态的,这并不做任何网络流量。

There is no direct support for such a thing but you can use udp sockets (SOCK_DGRAM) to connect to a known distant address. Since udp sockets are stateless this doesn't do any network traffic.

然后你就可以查询该插座( getsockname )的本地端点获得本地IP地址,通过该系统将路线。又由于UDP是无状态的这恰恰查询本地路由表,并没有真正的网络流量发出。

Then you can query for the local endpoint of that socket (getsockname) to obtain the local IP adress through which your system would route. Again since udp is stateless this just queries your local routing tables and no real network traffic is issued.

不要忘了在那之后关闭套接字。

Don't forget to close the socket after that.

远程公共域名服务器有利于这一目的。您可以测试其中几个

Remote public nameservers are good for that purpose. You can test for several of them


  • 其中一人可能偶尔会下降或者有可能是没有路由
    某些原因对世界的一部分

  • 您的过程可能是在同一个本地网络服务器和你
    可能只能获得一些特殊的本地地址

  • 测试IPv4和IPv6地址

这篇关于有没有让我的电脑的局域网IP地址的POSIX兼容的方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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