简体   繁体   English

通过 URL 获取 IP 地址

[英]Get IP-address by URL

This works这有效

target.sin_addr.s_addr = inet_addr("127.0.0.1");

but I want to put the IP from a website URL但我想从网站 URL 中获取 IP

I have tried我努力了

const char host[] = "http://www.google.com/";
struct hostent *host_ip;
host_ip = gethostbyaddr(host, strlen(host), 0);

I of corse did WSAStartup before I used gethostbyaddr();我在使用 gethostbyaddr() 之前做过 WSAStartup;

I've tried this我试过这个

target.sin_addr.s_addr = inet_addr(host_ip);

I've tried a few simmilar ones too but it isn't working.我也尝试了一些类似的,但它不起作用。 Could someone show me how to do this correctly.有人可以告诉我如何正确地做到这一点。

Thank you!谢谢!

EDIT:编辑:

When I do当我做

host_ip = gethostbyaddr((char *)&host, strlen(host), 0);
std::cout << host_ip->h_addr;

It gives me它给了我

httpa104-116-116-112.deploy.static.akamaitechnologies.com

inet_addr() accepts an IPv4 address string as input and returns the binary representation of that address. inet_addr()接受 IPv4 地址字符串作为输入并返回该地址的二进制表示。 That is not what you want in this situation, since you don't have a IP address, you have a hostname instead.在这种情况下,这不是您想要的,因为您没有 IP 地址,而是有一个主机名。

You are on the right track by using gethostby...() , but you need to use gethostbyname() (lookup by hostname) instead of gethostbyaddr() (lookup by IP address) 1 .您使用gethostby...()在正确的轨道上,但您需要使用gethostbyname() (按主机名查找)而不是gethostbyaddr() (按 IP 地址查找) 1 And you cannot pass a full URL to either of them.并且您不能将完整的 URL 传递给其中任何一个。 gethostbyname() takes only a hostname as input, so you need to parse the URL and extract its hostname, and then you can do something like this: gethostbyname()只需要一个主机名作为输入,因此您需要解析 URL 并提取其主机名,然后您可以执行以下操作:

const char host[] = ...; // an IP address or a hostname, like "www.google.com" by itself
target.sin_addr.s_addr = inet_addr(host);
if (target.sin_addr.s_addr == INADDR_NONE)
{
    struct hostent *phost = gethostbyname(host);
    if ((phost) && (phost->h_addrtype == AF_INET))
        target.sin_addr = *(in_addr*)(phost->h_addr);
    ...
}
else
    ...

1 BTW, the gethostby... () functions are deprecated, use getaddrinfo() and getnameinfo() instead. 1顺便说一句,不推荐使用gethostby... () 函数,请改用getaddrinfo()getnameinfo()

const char host[] = ...; // an IP address or a hostname, like "www.google.com" by itself

addrinfo hints = {0};
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

addrinfo *addr = NULL;

int ret = getaddrinfo(host, NULL, &hints, &addr);
if (ret == EAI_NONAME) // not an IP, retry as a hostname
{
    hints.ai_flags = 0;
    ret = getaddrinfo(host, NULL, &hints, &addr);
}
if (ret == 0)
{
    target = *(sockaddr_in*)(addr->ai_addr);
    freeaddrinfo(addr);
    ...
}
else
    ...

Try using getaddrinfo.尝试使用 getaddrinfo。 There's a quick guide to using it here: http://beej.us/guide/bgnet/output/html/multipage/getaddrinfoman.html这里有一个使用它的快速指南: http : //beej.us/guide/bgnet/output/html/multipage/getaddrinfoman.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM