通过域名获得IP

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

http://msdn.microsoft.com/en-us/library/windows/desktop/ms738524(v=vs.85).aspx


#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

int main(int argc, char **argv)
{

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;

    DWORD dwError;
    int i = 0;

    struct hostent *remoteHost;
    char *host_name;
    struct in_addr addr;

    char **pAlias;

    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s hostname\n", argv[0]);
        printf("  to return the IP addresses for the host\n");
        printf("       %s www.contoso.com\n", argv[0]);
        printf(" or\n");
        printf("       %s IPv4string\n", argv[0]);
        printf("  to return an IPv4 binary address for an IPv4string\n");
        printf("       %s 127.0.0.1\n", argv[0]);
        return 1;
    }
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    host_name = argv[1];

    printf("Calling gethostbyname with %s\n", host_name);
    remoteHost = gethostbyname(host_name);
    
    if (remoteHost == NULL) {
        dwError = WSAGetLastError();
        if (dwError != 0) {
            if (dwError == WSAHOST_NOT_FOUND) {
                printf("Host not found\n");
                return 1;
            } else if (dwError == WSANO_DATA) {
                printf("No data record found\n");
                return 1;
            } else {
                printf("Function failed with error: %ld\n", dwError);
                return 1;
            }
        }
    } else {
        printf("Function returned:\n");
        printf("\tOfficial name: %s\n", remoteHost->h_name);
        for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
            printf("\tAlternate name #%d: %s\n", ++i, *pAlias);
        }
        printf("\tAddress type: ");
        switch (remoteHost->h_addrtype) {
        case AF_INET:
            printf("AF_INET\n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS\n");
            break;
        default:
            printf(" %d\n", remoteHost->h_addrtype);
            break;
        }
        printf("\tAddress length: %d\n", remoteHost->h_length);

        i = 0;
        if (remoteHost->h_addrtype == AF_INET)
        {
            while (remoteHost->h_addr_list[i] != 0) {
                addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
                printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
            }
        }
        else if (remoteHost->h_addrtype == AF_NETBIOS)
        {   
            printf("NETBIOS address was returned\n");
        }   
    }

    return 0;
}

[gevent源码分析] c-ares异步DNS请求 c-ares是异步DNS请求库,libcurl,libevent,wireshark都使用了c-ares,gevent1.0版本前使用的是libevent, 所以它的DNS请求也是使用c-ares,1.0版本后使用cython封装了c-ares。c-ares官方文档,http://c-ares.haxx.se/docs.html。 阅读详情

相关推荐

c++ 通过域名获取ip(跨平台)

平常网络编程中,我们常会有需要获取某一个域名ip的需要,比如百度的域名为,但不知道它的ip地址,我们也没办法在代码中用socket连接上去。所以本文封装了一个跨平台的函数用于获取对应域名的所有ip地址,方便大家使用。

余识- 1442

C++---域名解析成IP地址

域名解析

weixin_45144862的博客 728

域名在Windows中的应用及示例代码

通过配置级域名和相应的资源记录,可以将域名划分为更小的子域,并将其映射到具体的网络资源。在示例代码中,首先添加了一个名为"subdomain"的级域名,并将其映射到IP地址"192.168.1.10"。在级域名下可以添加相应的主机记录(如A记录、CNAME记录等),用于将级域名映射到具体的网络资源。在该工具中,可以添加、编辑和删除域名以及相应的级域名。在DNS服务器管理工具中,选择要添加级域名域名,右键点击并选择“新建区域”。然后按照向导的提示,输入域名的名称和类型,完成级域名的添加。

FcUnity的博客 158

c-ares库和域名解析

c-ares库

南北粉面馆 686

java localhost 域名_JAVA获得域名IP地址的方法

本文实例讲述了JAVA获得域名IP地址的方法。分享给大家供大家参考。具体如下:import java.net.InetAddress;import java.net.UnknownHostException;public class TestInetAddress {InetAddress myIpAddress = null;InetAddress[] myServer = null;publi...

weixin_42551508的博客 469

如何获得IP地址,域名,计算机名等信息

程序中,想要实现一个根据用户提供的IP地址、域名或者计算机名等信息来验证用户的输入信息是否正确,但是一般用于是对于这些信息是模糊的,也许有人连计算机名是什么都不知道,那此时如何提供一个快捷的通道,来实现这样的验证信息呢,或者就像SQLServer一样,如何通过域认证来实现登录呢,这就和今天要说的内容相关,如果我能通过一些api默认得到域中的信息,就可以实现了类似的功能。 下面介绍相关的实现内容

1642

java 通过域名获取ip_通过域名获得IP地址

如果使用域名访问服务器,那么可能收到没有备案,或者因为是域名访问而走IDC防火墙的问题,所以能用IP地址访问总归是比用域名访问好的。但是如果使用IP访问,那么IP地址更换的时候,一些客户端就会遇到问题。为了解决IP地址更换和避免直接用域名访问的问题,我的做法是先解析域名IP,然后再通过IP进行访问。那么如何通过域名解析到IP呢?以下代码会帮到你:packagetest;importjava....

weixin_39566773的博客 1789

根据主机域名获得主机IP

根据域名(或者主机名)获得IP gethostbyname, 涉及到DNS解析 #include #include #include #include #include static const char *hostname2 = "baidu.com"; int main(int argc, char *argv[]) { struct hostent *host;

windseeker2008的专栏 746

linux 通过域名获得ip

代码 #include #include int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage:%s domain_name\n", argv[0]); return 1; } str

zzz_781111的专栏 4166

java域名获取ip_JAVA获得域名IP地址的方法

本文实例讲述了java获得域名ip地址的方法。分享给大家供大家参考。具体如下:import java.net.inetaddress;import java.net.unknownhostexception;public class testinetaddress {inetaddress myipaddress = null;inetaddress[] myserver = null;publi...

weixin_39603908的博客 587

通过java解析域名获得IP地址

IP地址是Internet主机的作为路由寻址用的数字型标识,人不容易记忆。因而产生了域名(domain name)这一种字符型标识。DNS即为域名解析服务。在这里我们如果想通过java程序来解析域名获得真实IP的话,可以通过java的InetAddress类来实现。 import java.net.InetAddress; import java.net.UnknownHostExceptio...

sinat_35566306的博客 3471

通过DNS解析域名获得ip

#include<stdio.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include<netinet/in.h> #include <unistd.h> #inc

qq_43701013的博客 969

java 域名 ip_JAVA获得域名IP地址的方法

import java.net.InetAddress;import java.net.UnknownHostException;public class TestInetAddress {InetAddress myIpAddress = null;InetAddress[] myServer = null;public static void main(String args[]) {Test...

weixin_39896839的博客 201

JAVA获得域名IP地址

<br /> <br />  import java.net.InetAddress;<br />  import java.net.UnknownHostException;<br />  public class TestInetAddress {<br />  InetAddress myIpAddress = null;<br />  InetAddress[] myServer = null;<br />  public static void main(String args[]) {<br /

lupeng0527的专栏 2758

通过域名获得主机ip

通过域名获得主机IP地址: #include #include #include #include #include #include int main(int argc,char **argv) { struct hostent *hent; int i; hent = gethostbyname("baidu.com"); printf("host

yanghui07216的博客 455

java 获得ip 端口 域名

获得ip域名和端口号

yz18931904的博客 865

【qt】如何通过域名获得IP地址?

如何通过域名获得IP地址?

qq_74047911的博客 584
上一篇: 启动Telnet功能
下一篇: 基于TCP连接的C S模式的最简单模型代码
小大小丑
博客等级 码龄18年 483粉丝 425原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值