vc根据域名获取IP地址 gethostbyname()函数

vc根据域名获取IP地址 gethostbyname()函数

 

 

以下是VC Socket初始化时用到的两个函数

一、WSAStartup函数
                int WSAStartup
                       (
                          WORD wVersionRequested,
                          LPWSADATA lpWSAData
                         );
使用Socket的程序在使用Socket之前必须调用WSAStartup函数。该函数的第一个参数指明程序请求使用的Socket版本,其中高位字节指明副版本、低位字节指明主版本;操作系统利用第二个参数返回请求的Socket的版本信息。当一个应用程序调用WSAStartup函数时,操作系统根据请求的Socket版本来搜索相应的Socket库,然后绑定找到的Socket库到该应用程序中。以后应用程序就可以调用所请求的Socket库中的其它Socket函数了。该函数执行成功后返回0。
例:假如一个程序要使用2.1版本的Socket,那么程序代码如下
wVersionRequested = MAKEWORD( 2, 1 );
err = WSAStartup( wVersionRequested, &wsaData );

二、WSACleanup函数
int WSACleanup (void);
应用程序在完成对请求的Socket库的使用后,要调用WSACleanup函数来解除与Socket库的绑定并且释放Socket库所占用的系统资源。
三 Socket接口的检索有关域名、通信服务和协议等Internet信息的数据库函数,如

gethostbyaddr、gethostbyname、gethostname、getprotolbyname

getprotolbynumber、getserverbyname、getservbyport。
 1.gethostname()

  【函数原型】

int PASCAL FAR gethostname (char FAR * name, int namelen);

  【使用说明】

  该函数可以获取本地主机的主机名,其中:

  name:<输出>用于指向所获取的主机名的缓冲区的指针。

  Namelen:<输入>缓冲区的大小,以字节为单位。

  返回值:若无错误,返回0;否则,返回错误代吗。
2.gethostbyname()

  【函数原型】

struct hostent FAR * PASCAL FAR gethostbyname(const char FAR * name);

  【使用说明】

  该函数可以从主机名数据库中得到对应的“主机”。

  该函数唯一的参数name就是前面调用函数gethostname()得到的主机名。若无错误,刚返回一个指向hostent结构的批针,它可以标识一个“主机”列表。

 

===============================================

根据域名 获取IP地址 gethostbyname()函数

可以运行的源代码

很容易, 但让一个没用过的人就不好写, 那些高手都很懒写BLOG
:

//getip.cpp
#include <windows.h>
#include <iostream>
#include <winsock.h>
#pragma comment(lib, "ws2_32")

using namespace std;
int main()
{

 int WSA_return;
 WSADATA WSAData;

 
 WSA_return=WSAStartup(0x0101,&WSAData);

 
 HOSTENT *host_entry;

 
 char host_name[256] ="http://www.google.cn/";

 if(WSA_return==0)
 {
      
  host_entry=gethostbyname(host_name);
  printf("%s/n", host_name);
  if(host_entry!=0)
  {
   printf("解析IP地址: ");
    printf("%d.%d.%d.%d",
        (host_entry->h_addr_list[0][0]&0x00ff),
        (host_entry->h_addr_list[0][1]&0x00ff),
        (host_entry->h_addr_list[0][2]&0x00ff),
        (host_entry->h_addr_list[0][3]&0x00ff));

  }
 }
 WSACleanup();

 

 

下面有几份不同功能的代码:

/*#include   <windows.h>
#include   <winsock.h>
#include   <cstdio>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void   main()
{
 WSADATA     wsaData;
 char           szHostname[100]="baidu.com";
 HOSTENT   *pHostEnt;
 int             nAdapter   =   0;
 struct       sockaddr_in   sAddr;

 if   (WSAStartup(0x0101,   &wsaData))
 {
  printf( "WSAStartup   failed   %s/n ",   WSAGetLastError());
  return;
 }

 pHostEnt   =   gethostbyname(szHostname);

 while   (   pHostEnt-> h_addr_list[nAdapter]   )
 {
  //   pHostEnt-> h_addr_list[nAdapter]   is   the   current   address   in   host
  //   order.

  //   Copy   the   address   information   from   the   pHostEnt   to   a   sockaddr_in
  //   structure.
  memcpy   (   &sAddr.sin_addr.s_addr,   pHostEnt-> h_addr_list[nAdapter],
   pHostEnt-> h_length);

  //   Output   the   machines   IP   Address.
  printf( "Name:         %s/nAddress:   %s/n ",   pHostEnt-> h_name,
   inet_ntoa(sAddr.sin_addr));

  nAdapter++;
 }
 WSACleanup();
 return;

}
*/
/*
#include <WINSOCK2.H>
#include <stdio.h>
#pragma comment(lib,"WS2_32.LIB")

void TestFunOne(void)
{
 SOCKET m_socket=socket(AF_INET,SOCK_STREAM,0);

 SOCKADDR_IN addr;
 addr.sin_family=AF_INET;
 addr.sin_port=htons(80);
 addr.sin_addr.S_un.S_addr=inet_addr("202.108.22.5");

 if(connect(m_socket,(SOCKADDR *)&addr,sizeof(SOCKADDR))!=SOCKET_ERROR)
 {
  printf("连接百度成功! TestFunOne../n");
 }
 else
 {
  printf("连接百度失败! TestFunOne../n");
 }
 closesocket(m_socket);
}

DWORD WINAPI TestThreadOne(LPVOID lparameter)
{

 SOCKET m_socket=socket(AF_INET,SOCK_STREAM,0);

 SOCKADDR_IN addr;
 addr.sin_family=AF_INET;
 addr.sin_port=htons(80);
 addr.sin_addr.S_un.S_addr=inet_addr("202.108.22.5");

 if(connect(m_socket,(SOCKADDR *)&addr,sizeof(SOCKADDR))!=SOCKET_ERROR)
 {
  printf("连接百度成功! thread one../n");
 }
 else
 {
  printf("连接百度失败! thread one../n");
 }
 closesocket(m_socket);
 TestFunOne();
 return 1;
}


int main(int argc, char* argv[])
{
 WSAData wsa;
 WSAStartup(MAKEWORD(2,2),&wsa);

 CloseHandle(CreateThread(NULL,NULL,TestThreadOne,NULL,NULL,NULL));

 SOCKET m_socket=socket(AF_INET,SOCK_STREAM,0);

 SOCKADDR_IN addr;
 addr.sin_family=AF_INET;
 addr.sin_port=htons(80);
 addr.sin_addr.S_un.S_addr=inet_addr("202.108.22.5");

 if(connect(m_socket,(SOCKADDR *)&addr,sizeof(SOCKADDR))!=SOCKET_ERROR)
 {
  printf("连接百度成功! main thread../n");
 }
 else
 { againstvirus

  printf("连接百度失败! main thread../n");
 }
 closesocket(m_socket);
 WSACleanup();
 Sleep(20000000);
 return 0;
}
*/

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>


#pragma comment(lib, "ws2_32.lib")

 

int main(int argc, char  **argv)

{   
 //-----------------------------------------

 // 定义初始化变量

 WSADATA wsaData;   
 int iResult;
 DWORD dwError;

 int i = 0;   
 struct hostent *remoteHost;   
 char *host_name;   
 struct in_addr addr;   
 char **pAlias;

 // 校验参数

 if (argc != 2) {       
  printf("usage: %s ipv4address/n", argv[0]);       
  printf(" or/n");       
  printf("       %s hostname/n", argv[0]);       
  printf("  to return the host/n");       
  printf("       %s 127.0.0.1/n", argv[0]);       
  printf("  to return the IP addresses for a host/n");       
  printf("       %s www.a3gs.com/n", argv[0]);       
  return 1;
 }

 // 初始化Winsock

 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);   
 if (iResult != 0) {       
  printf("WSAStartup failed: %d/n", iResult);       
  return 1;       
 }

 host_name = argv[1];
 // 如果用户输入是一个字母, 则我们使用gethostbyname()函数
 // 假如输入是数字,则判断为IP地址
 if (isalpha(host_name[0])) {   
  printf("Calling gethostbyname with %s/n", host_name);       
  remoteHost = gethostbyname(host_name);   
 } else {       
  printf("使用%s调用gethostbyaddr函数%s/n", host_name);   
  addr.s_addr = inet_addr(host_name);   
  if (addr.s_addr == INADDR_NONE) {       
   printf("请输入有效IPv4地址/n");   
   return 1;   
  } else   
  {   
   remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);   
  }
 }
 if (remoteHost == NULL) {   
  dwError = WSAGetLastError();   
  if (dwError != 0) {   
   if (dwError == WSAHOST_NOT_FOUND) {       
    printf("无法定位到主机/n");
    return 1;
   }else if (dwError == WSANO_DATA) {       
    printf("无路由记录/n");   
    return 1;   
   } else {
    printf("函数调用出错,代码: %ld/n", dwError);
    return 1;   
   }
  }
 } else {
  printf("函数返回:/n");
  printf("/t主机名称: %s/n", remoteHost->h_name);
  for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
   printf("/t主机别名#%d: %s/n", ++i, *pAlias);
  }
  printf("/t地址类型: ");
  switch (remoteHost->h_addrtype) {
  case AF_INET:   
   printf("AF_INET/n");
   break;
  case AF_INET6:
   printf("AF_INET6/n");
   break;
  case AF_NETBIOS:
   printf("AF_NETBIOS/n");
   break;
  default:
   printf(" %d/n", remoteHost->h_addrtype);
   break;
  }
  printf("/t地址长度: %d/n", remoteHost->h_length);     
  i = 0;
  while (remoteHost->h_addr_list[i] != 0) {
   addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
   printf("/t主机IP地址#%d: %s/n", i, inet_ntoa(addr));
  }   
 }
 return 0;
}

 

 

 

 

 

#include <stdafx.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>


#pragma comment(lib, "ws2_32.lib")


int main(int argc, char  **argv)

{    
    //-----------------------------------------
    
    // 定义初始化变量
    
    WSADATA wsaData;    
    int iResult;
    DWORD dwError;
    
    int i = 0;    
    struct hostent *remoteHost;    
    char *host_name;    
    struct in_addr addr;    
    char **pAlias;
    
    // 校验参数
    
    if (argc != 2) {        
        printf("usage: %s ipv4address/n", argv[0]);        
        printf(" or/n");        
        printf("       %s hostname/n", argv[0]);        
        printf("  to return the host/n");        
        printf("       %s 127.0.0.1/n", argv[0]);        
        printf("  to return the IP addresses for a host/n");        
        printf("       %s www.a3gs.com/n", argv[0]);        
        return 1;
    }
    
    // 初始化Winsock
    
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);    
    if (iResult != 0) {        
        printf("WSAStartup failed: %d/n", iResult);        
        return 1;        
    }

    host_name = argv[1];
    // 如果用户输入是一个字母, 则我们使用gethostbyname()函数
    // 假如输入是数字,则判断为IP地址
    if (isalpha(host_name[0])) {    
        printf("Calling gethostbyname with %s/n", host_name);        
        remoteHost = gethostbyname(host_name);    
    } else {        
        printf("使用%s调用gethostbyaddr函数%s/n", host_name);    
        addr.s_addr = inet_addr(host_name);    
        if (addr.s_addr == INADDR_NONE) {        
            printf("请输入有效IPv4地址/n");    
            return 1;    
        } else    
        {    
            remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);    
        }
    }
    if (remoteHost == NULL) {    
        dwError = WSAGetLastError();    
        if (dwError != 0) {    
            if (dwError == WSAHOST_NOT_FOUND) {        
                printf("无法定位到主机/n");
                return 1;
            }else if (dwError == WSANO_DATA) {        
                printf("无路由记录/n");    
                return 1;    
            } else {
                printf("函数调用出错,代码: %ld/n", dwError);
                return 1;    
            }
        }
    } else {
        printf("函数返回:/n");
        printf("/t主机名称: %s/n", remoteHost->h_name);
        for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
            printf("/t主机别名#%d: %s/n", ++i, *pAlias);
        }
        printf("/t地址类型: ");
        switch (remoteHost->h_addrtype) {
        case AF_INET:    
            printf("AF_INET/n");
            break;
        case AF_INET6:
            printf("AF_INET6/n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS/n");
            break;
        default:
            printf(" %d/n", remoteHost->h_addrtype);
            break;
        }
        printf("/t地址长度: %d/n", remoteHost->h_length);      
        i = 0;
        while (remoteHost->h_addr_list[i] != 0) {
            addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
            printf("/t主机IP地址#%d: %s/n", i, inet_ntoa(addr));
        }    
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值