码迷,mamicode.com
首页 > 编程语言 > 详细

域名解析成IP地址C++

时间:2015-03-11 19:08:48      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

#include <windows.h>

 

#include <iostream>

 

#include <winsock.h>

 

#pragma comment(lib, "ws2_32")

 

#include <sstream>

 

#include<string>

 

using namespace std;

 

void main()

 

{

 

int WSA_return;

 

WSADATA WSAData;

 

WSA_return=WSAStartup(0x0101,&WSAData);

 

/* 结构指针 */

 

char ch;

 

HOSTENT *host_entry;

 

cout<<"如果您已经输入完毕,请直接关闭退出"<<endl;

 

/*设置循环不断的读入*/

 

do{

 

char host_name[256];

 

cout<<"请您输入要解析的网址:";

 

cin.getline(host_name,256);

 

if(WSA_return==0)

 

{

 

/* 即要解析的域名或主机名 */

 

host_entry=gethostbyname(host_name);

 

printf("%s\n", host_name);

 

if(host_entry!=0)

 

{

 

string str1,str2,str3,str4,str5;

 

int i1=host_entry->h_addr_list[0][0]&0x00ff,

 

i2=host_entry->h_addr_list[0][1]&0x00ff,

 

i3=host_entry->h_addr_list[0][2]&0x00ff,

 

i4=host_entry->h_addr_list[0][3]&0x00ff;

 

stringstream strconvert1,strconvert2,strconvert3,strconvert4;

 

strconvert1<<i1;//把int型转换成string类型的数据

 

strconvert1>>str1;

 

strconvert2<<i2;

 

strconvert2>>str2;

 

strconvert3<<i3;

 

strconvert3>>str3;

 

strconvert4<<i4;

 

strconvert4>>str4;

 

str5=str1+‘.‘+str2+‘.‘+str3+‘.‘+str4;

 

cout<<"以字符串形式输出:"<<str5<<endl;

 

printf("解析IP地址: ");

 

printf("%d.%d.%d.%d\n",

 

(host_entry->h_addr_list[0][0]&0x00ff),//得到的id

 

(host_entry->h_addr_list[0][1]&0x00ff),

 

(host_entry->h_addr_list[0][2]&0x00ff),

 

(host_entry->h_addr_list[0][3]&0x00ff));

 

}

 

}

 

}while(ch!=27);

 

WSACleanup();

 

}

 

/*使用Socket的程序在使用Socket之前必须调用WSAStartup函数。

 

该函数的第一个参数指明程序请求使用的Socket版本,

 

其中高位字节指明副版本、低位字节指明主版本;

 

操作系统利用第二个参数返回请求的Socket的版本信息。

 

当一个应用程序调用WSAStartup函数时,操作系统根据请求的Socket版本来搜索相应的Socket库,

 

然后绑定找到的Socket库到该应用程序中。

 

以后应用程序就可以调用所请求的Socket库中的其它Socket函数了。

 

该函数执行成功后返回0*/

域名解析成IP地址C++

标签:

原文地址:http://www.cnblogs.com/xiaxiaosheng/p/4330641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!