欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

c/c++實現(xiàn)獲取域名的IP地址

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

c/c++實現(xiàn)獲取域名的IP地址

// GetHostIP.cpp : 定義控制臺應用程序的入口點。
//
 
#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)
{
 
  //-----------------------------------------
  // Declare and initialize variables
  /*
  *  WSADATA結構被用來儲存調(diào)用AfxSocketInit全局函數(shù)返回的Windows Sockets初始化信息。
  *  這個結構被用來存儲被WSAStartup函數(shù)調(diào)用后返回的Windows Sockets數(shù)據(jù)。它包含Winsock.dll執(zhí)行的數(shù)據(jù)。
  */
  WSADATA wsaData;
  int iResult;
 
  DWORD dwError; /*每個word為2個字節(jié)的長度,DWORD 雙字即為4個字節(jié),每個字節(jié)是8位*/
  int i = 0;
 
  struct hostent *remoteHost;     /*域名*/
  char *host_name;          /*主機名*/
  struct in_addr addr;        /*ip*/
 
  char **pAlias;
 
  // Validate the parameters
  if (argc != 2) {
    printf("usage: GetHostIP hostname\n");
    return 1;
  }
  // 此處應添加的程序如下
  // 1. 當初始化winsocket
  iResult = WSAStartup(MAKEWORD(2,2),&wsaData);/* & 取地址*/
  // 2. 檢查該socket是否初始化成功,即該socket是否等于0;如果初始化不成功,應當給出錯誤報警,并結束程序。
  if(iResult!=0){
    printf("初始化失敗!\n");
    return 1;
  }
 
  /////////////////結束///////////////////////////////////
 
 
  host_name = argv[1];
  printf("Calling gethostbyname with %s\n", host_name);
  // 此處應添加的程序如下
  // 1. 利用函數(shù)gethostbyname(),獲取給定主機名的指針。
  remoteHost = gethostbyname(host_name);
  // 2. 應當熟悉該結構指針的結構
  // 其中該函數(shù)申明如下:struct hostent* gethostbyname(const char *name)
 
   
  // 此處應添加的程序如下
  // 1. 如果上面函數(shù)返回的主機結構指針為空(NULL),則做如下處理:
  //   a. 利用函數(shù) int WSAGetLastError ( void ) 檢查當前是否發(fā)生網(wǎng)絡錯誤,
  //   b. 返回的發(fā)生的錯誤類型并作相應的處理,比如,若沒有找到主機的錯誤(此時該函數(shù)返回WSAHOST_NOT_FOUND)
  if(remoteHost == NULL){
    //printf("gethostbynameError:%d",WSAGetLastError());
    return 1;
  }else{
  // 2. 如果返回的主機指針不為空,則做如下處理:
  //   a. 打印出如下參數(shù):主機名和IP地址,若該主機對應于多個ip地址,應當分別列出。
    printf("主機名:%s\n",remoteHost->h_name);
    for(i=0;;i++){
      if(remoteHost->h_addr_list[i]!=0)
      {
      /*從緩存中把 p 拷貝到addr中
      *同時addr.S_un.Saddr
      * in_addr ipAddr; 
      * ipAddr.S_un.S_addr = inet_addr("127.0.0.1"); 
      * 就是把字符串形式的ip地址轉化為0xXXXXXXXX形式的地址格式。
      */
        addr.s_addr = *(u_long*)remoteHost->h_addr_list[i]; 
        printf("ip #%d:%s\n",i,inet_ntoa(addr)); /* inet_ntoa() 函數(shù)將網(wǎng)絡地址轉成二進制的數(shù)字相關函數(shù):inet_aton, inet_ntoa */
      }
    /*
    for(i=0;;i++){
      char *p = remoteHost->h_addr_list[i];
      if(p==NULL) break;
      /*從緩存中把 p 拷貝到addr中
      *同時addr.S_un.Saddr
      * in_addr ipAddr; 
      * ipAddr.S_un.S_addr = inet_addr("127.0.0.1"); 
      * 就是把字符串形式的ip地址轉化為0xXXXXXXXX形式的地址格式。
      *
      memcpy(&addr.S_un.S_addr,p,remoteHost->h_length); 
      printf("ip地址為:%s\n",inet_ntoa(addr));
     
    */
     
    }
     
  }
   
 
 
  // 此處應添加的程序如下
  // 程序完成后應當適當測試,需要進行的測試如下:
  // 1. 測試主機結構指針獲取失敗
  // 2. 測試包含多個IP地址的主機
  // 3. 你能想到的任何可能出現(xiàn)的異常情況
  /////////////////結束///////////////////////////////////
   
   
  system("pause"); /*防止窗體關閉函數(shù)*/
  return 0;
}

下面給大家詳細介紹下上面代碼的核心gethostbyname的詳細用法

使用這個東西,首先要包含2個頭文件:

#include <netdb.h>
#include <sys/socket.h>
struct hostent *gethostbyname(const char *name);

這個函數(shù)的傳入值是域名或者主機名,例如"www.google.com","wpc"等等。
傳出值,是一個hostent的結構(如下)。如果函數(shù)調(diào)用失敗,將返回NULL。

struct hostent {
   char *h_name;
   char **h_aliases;
   int h_addrtype;
   int h_length;
   char **h_addr_list;
};

解釋一下這個結構, 其中:

char *h_name 表示的是主機的規(guī)范名。例如www.google.com的規(guī)范名其實是www.l.google.com。
char **h_aliases 表示的是主機的別名。www.google.com就是google他自己的別名。有的時候,有的主機可能有好幾個別名,這些,其實都是為了易于用戶記憶而為自己的網(wǎng)站多取的名字。
int h_addrtype 表示的是主機ip地址的類型,到底是ipv4(AF_INET),還是ipv6(AF_INET6)
int h_length 表示的是主機ip地址的長度
int **h_addr_lisst 表示的是主機的ip地址,注意,這個是以網(wǎng)絡字節(jié)序存儲的。千萬不要直接用printf帶%s參數(shù)來打這個東西,會有問題的哇。所以到真正需要打印出這個IP的話,需要調(diào)用inet_ntop()。
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
這個函數(shù),是將類型為af的網(wǎng)絡地址結構src,轉換成主機序的字符串形式,存放在長度為cnt的字符串中。
這個函數(shù),其實就是返回指向dst的一個指針。如果函數(shù)調(diào)用錯誤,返回值是NULL。
下面是例程,有詳細的注釋。

#include <netdb.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
char *ptr,**pptr;
struct hostent *hptr;
char str[32];
/* 取得命令后第一個參數(shù),即要解析的域名或主機名 */
ptr = argv[1];
/* 調(diào)用gethostbyname()。調(diào)用結果都存在hptr中 */
if( (hptr = gethostbyname(ptr) ) == NULL )
{
printf("gethostbyname error for host:%s/n", ptr);
return 0; /* 如果調(diào)用gethostbyname發(fā)生錯誤,返回1 */
}
/* 將主機的規(guī)范名打出來 */
printf("official hostname:%s/n",hptr->h_name);
/* 主機可能有多個別名,將所有別名分別打出來 */
for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
printf(" alias:%s/n",*pptr);
/* 根據(jù)地址類型,將地址打出來 */
switch(hptr->h_addrtype)
{
case AF_INET:
case AF_INET6:
pptr=hptr->h_addr_list;
/* 將剛才得到的所有地址都打出來。其中調(diào)用了inet_ntop()函數(shù) */
for(;*pptr!=NULL;pptr++)
printf(" address:%s/n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
break;
default:
printf("unknown address type/n");
break;
}
return 0;
} 

另外附上獲得公網(wǎng)與內(nèi)網(wǎng)ip的代碼:

bool getPublicIp(string& ip)
{
  int  sock;
  char **pptr = NULL;
  struct sockaddr_in  destAddr;
  struct hostent  *ptr = NULL;
  char destIP[128];

  sock = socket(AF_INET,SOCK_STREAM,0);
  if( -1 == sock ){
    perror("creat socket failed");
    return false;
  }
  bzero((void *)&destAddr,sizeof(destAddr));
  destAddr.sin_family = AF_INET;
  destAddr.sin_port = htons(80);
  ptr = gethostbyname("www.ip138.com");
  if(NULL == ptr){
    perror("gethostbyname error");
    return false;
  }
  for(pptr=ptr->h_addr_list ; NULL != *pptr ; ++pptr){
    inet_ntop(ptr->h_addrtype,*pptr,destIP,sizeof(destIP));
    printf("addr:%s\n",destIP);
    ip = destIP;
    return true;
  }
  return true;
}

獲取內(nèi)網(wǎng)IP

int getlocalip(char* outip)
{
#ifndef WIN32
	int i=0;
	int sockfd;
	struct ifconf ifconf;
	char buf[512];
	struct ifreq *ifreq;
	char* ip;
	//初始化ifconf
	ifconf.ifc_len = 512;
	ifconf.ifc_buf = buf;
	strcpy(outip,"127.0.0.1");
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
	{
		return -1;
	}
	ioctl(sockfd, SIOCGIFCONF, &ifconf);  //獲取所有接口信息
	close(sockfd);
	//接下來一個一個的獲取IP地址
	ifreq = (struct ifreq*)buf;
	for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--)
	{
		ip = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);


		if(strcmp(ip,"127.0.0.1")==0) //排除127.0.0.1,繼續(xù)下一個
		{
			ifreq++;
			continue;
		}
	}
	strcpy(outip,ip);
	return 0;
#else
	return 0;
#endif
	
}

上一篇:C++中strcpy函數(shù)的實現(xiàn)

欄    目:C語言

下一篇:深入淺析STL vector用法

本文標題:c/c++實現(xiàn)獲取域名的IP地址

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2661.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有