如何使用 gethostbyname() 检测您的 IP/名称

发布于 2024-12-18 02:20:19 字数 867 浏览 1 评论 0原文

有没有办法从gethostname()获取IP号码?

我们为实验室中的计算机随机生成 IP 地址。我们使用 gethostbyname() 来获取计算机的 IP。

我们本质上要做的就是比较从 gethostbyname() 获得的 ip 与从 gethostname() 获得的 IP。

我们尝试过:

struct hostent* host;
char temp[MAX_LEN];
gethostname(temp, MAX_LEN);

host = gethostbyname(<random ip address>)

if(host->h_name == temp) printf("They are the same\n");

问题是,host->h_name 是 '172.125.45.1' (我这样做了 向上),温度为'u-my_comp'

所以我们无法比较字符串,因为一个给出了计算机的名称 (u-my_comp),另一个给出了 ip...

无论如何使这些函数返回相同类型的值?

我们尝试过做类似的事情,

gethostname(temp, 24)
temp_host = gethostbyname(temp)

希望现在我们可以将 temp_host->h_name 与 host->h_name 进行比较......但是,是的,这也不起作用。

有什么想法吗?

谢谢!

is there a way to get the IP number from gethostname()?

We are randomly generating IP addresses for the computers in the lab we are in. We use gethostbyname(<random ip>) to get the IP of a computer.

What we want to do essentially is compare the ip that we get from gethostbyname() with what we get from gethostname().

We tried:

struct hostent* host;
char temp[MAX_LEN];
gethostname(temp, MAX_LEN);

host = gethostbyname(<random ip address>)

if(host->h_name == temp) printf("They are the same\n");

The problem is, is that host->h_name is '172.125.45.1' (i made that
up) and temp is 'u-my_comp'

so we cant compare the strings cause one gives us the name of the computer (u-my_comp), and the other gives the ip...

Is there anyway to make these functions return the same type of value?

we have tried doing something like

gethostname(temp, 24)
temp_host = gethostbyname(temp)

in hopes that now we could compare temp_host->h_name with host->h_name...but yeah, that didnt work either.

Any ideas?

thanks!

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

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

发布评论

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

评论(2

蹲在坟头点根烟 2024-12-25 02:20:19

gethostbyname() 用于将主机名转换为套接字地址。如果您提供的“主机名”是一个点分四组 IPv4 地址,那么这将是您在结果的 h_name 参数中获得的全部内容。

要将套接字地址转换回名称,您需要使用配套函数 gethostbyaddr() - 但您不需要,因为 gethostbyname()gethostbyaddr() 已弃用。相反,您应该使用 getaddrinfo()getnameinfo()

例如:

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    struct sockaddr_in sa;
    char host[1024];
    int gni_err;

    sa.sin_family = AF_INET;
    sa.sin_port = 0;
    sa.sin_addr.s_addr = inet_addr("127.0.0.1");

    gni_err = getnameinfo((struct sockaddr *)&sa, sizeof sa, host, sizeof host, NULL, 0, NI_NAMEREQD | NI_NOFQDN);

    if (gni_err == 0) {
        printf("host is: %s\n", host);
    } else {
        fprintf(stderr, "Error looking up host: %s\n", gai_strerror(gni_err));
    }

    return 0;
}

gethostbyname() is for converting a hostname into a socket address. If the "hostname" you supply is a dotted-quad IPv4 address, that will be all you get in the h_name parameter of the result.

To convert a socket address back into a name what you want is the companion function gethostbyaddr() - except that you don't, because both gethostbyname() and gethostbyaddr() are deprecated. Instead, you should be using getaddrinfo() and getnameinfo().

For example:

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    struct sockaddr_in sa;
    char host[1024];
    int gni_err;

    sa.sin_family = AF_INET;
    sa.sin_port = 0;
    sa.sin_addr.s_addr = inet_addr("127.0.0.1");

    gni_err = getnameinfo((struct sockaddr *)&sa, sizeof sa, host, sizeof host, NULL, 0, NI_NAMEREQD | NI_NOFQDN);

    if (gni_err == 0) {
        printf("host is: %s\n", host);
    } else {
        fprintf(stderr, "Error looking up host: %s\n", gai_strerror(gni_err));
    }

    return 0;
}
江南烟雨〆相思醉 2024-12-25 02:20:19

如果您调用:(

myhost = gethostbyname(temp);

已分配 myhost),那么您将有两个要比较的 hostent 结构 - 您将获得目标查询主机和当前主机的 IP 地址列表(而不仅仅是当前主机的主机名)。

If you call:

myhost = gethostbyname(temp);

(having allocated myhost) then you will have two hostent structures you will compare - you will have the IP address lists for both the target query host and the current host (not just the hostname for the current host).

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