如何使用套接字地址获取自己的IP地址? [英] How to get its own IP address with a socket address?

查看:210
本文介绍了如何使用套接字地址获取自己的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取启动程序的计算机的IP地址,然后将其发送到客户端,但是我总是得到0.0.0.1而不是真实的IP地址(例如127.0.0.1) ).

I want to get the IP address of the computer my program is launched on, to be able then to send it to a client, but I always get 0.0.0.1 instead of the real IP address (like 127.0.0.1 for instance).

我目前能够获取端口,但无法获取IP地址.

I'm currently able to get the port, but not the IP address.

我怎么得到它?

最好的解决方案是使用sockaddr_in进行获取.这是我目前正在做的事情:

The best solution would be to be able to get it with a sockaddr_in. Here's what I'm currently doing:

int open_connection(char* ip, int* port)
{
  int                   sock;
  struct sockaddr_in    sin;
  socklen_t             len;
  int                   i;

  i = 0;
  len = sizeof(sin);
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return (-1);
  bzero(&sin, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) != 0)
    perror("Error on bind");
  if (getsockname(sock, (struct sockaddr *)&sin, &len) != 0)
    perror("Error on getsockname");
  strcpy(ip, inet_ntoa(sin.sin_addr)); // IP = 0.0.0.0
  *port = sin.sin_port;
  return (sock);
}

编辑:我知道我的思维方式走错了路.所以我的问题是:获得自己的IP地址的最佳方法是什么?

I understand I was going on the wrong way with my way of thinking. So my question is: What's the best way to get your own IP address?

推荐答案

当您将套接字bind()设置为0.0.0.0时,这是该套接字在调用getsockname()时可用的唯一IP.这意味着套接字已绑定到所有本地接口.为了从套接字获取特定IP,必须将其绑定到特定IP.

When you bind() a socket to 0.0.0.0, that is the only IP the socket has available when calling getsockname(). It means the socket is bound to all local interfaces. In order to get a specific IP from a socket, it has to be bound to a specific IP.

使用套接字API获取计算机的本地IP仍然是错误的方法.一个常见的错误是将gethostname()gethostbyname()getaddrinfo()一起使用以获取本地IP列表.通常这是可行的,但是它有一些隐藏的陷阱可能导致虚假信息,但是人们倾向于忽略这一事实,或者甚至一开始都不了解(我多年来都不知道这一点,但是后来我学得更好).

Using the socket API to get the machine's local IP(s) is the wrong approach anyway. A common mistake is to use gethostname() with gethostbyname() or getaddrinfo() to get the local IP list. Usually that works, but it has some hidden gotchas that can cause false information, but people tend to ignore that fact, or don't even know about it in the first place (I didn't know about it for years, but then I learned better).

实际上,您确实应该使用特定于平台的API来枚举本地网络接口.这将提供更可靠的信息. Windows具有GetAdaptersInfo()GetAdaptersAddresses().其他平台具有getifaddrs().这些将告诉您哪些本地IP可用.然后,您可以bind()设置为0.0.0.0的套接字以接受任何这些IP上的客户端,或者bind()设置为特定的IP以仅接受该IP上的客户端.

Instead, you really should use platform-specific APIs for enumerating the local networking interfaces. That will provide more reliable information. Windows has GetAdaptersInfo() and GetAdaptersAddresses(). Other platforms have getifaddrs(). Those will tell you what local IPs are available. You can then bind() a socket to 0.0.0.0 in order to accept clients on any of those IPs, or bind() to a specific IP to accept clients only on that IP.

这篇关于如何使用套接字地址获取自己的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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