简体   繁体   中英

getaddrinfo(): IP address of domain registered with No-IP

I want to connect a client and a server through the internet (using C++ and winsock2), so I registered a domain name for the server on No-IP, so that the client could determine it's IP address without both having to be on the same LAN. But when the client uses getaddrinfo() to determine the server IP address from the domain name, getaddrinfo() always returns 8.23.224.90 (which is not the server's IP address). Ping statistics also show that the domain name's IP address 8.23.224.90. After I googled the problem for a long time I eventually found this on the No-IP website:

"The IP addresses 8.23.224.90 and 50.19.220.154 are the direct locations for our web redirect servers. If you have a hostname setup as a web redirect, port 80 redirect or have an offline page enabled, these are the IPs that will be assigned to your host."

Currently this is how the client gets the IP address from domain name "hostname". What should I add/change to get the real IP address? Thanks in advance...

int hostname_to_ip(char *hostname, char *ip) {

    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_in *h;

    memset(&hints,0,sizeof(hints));
    hints.ai_family=AF_INET;
    hints.ai_socktype=SOCK_STREAM;

    getaddrinfo(hostname,NULL,&hints,&servinfo);
    for (p=servinfo; p!=NULL; p=p->ai_next) {
        h=(struct sockaddr_in*)p->ai_addr;
        strcpy(ip,inet_ntoa(h->sin_addr));
    }

}

Don't set up your hostname as a web redirect, but rather as a DNS A record. More details on their website .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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