Domain name resolved to IP address C + +

Source: Internet
Author: User

#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);

/* Structure pointer */

Char ch;

Hostent *host_entry;

cout<< "If you have already entered, please close the exit" <<endl;

/* Set the loop to read in and out continuously */

do{

Char host_name[256];

cout<< "Please enter the URL you want to parse:";

Cin.getline (host_name,256);

if (wsa_return==0)

{

/* is the domain name or hostname to resolve */

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;//converts an int to a string-type data

strconvert1>>str1;

strconvert2<<i2;

strconvert2>>str2;

strconvert3<<i3;

strconvert3>>str3;

strconvert4<<i4;

strconvert4>>str4;

str5=str1+ '. ' +str2+ '. ' +str3+ '. ' +STR4;

cout<< "Output as a string:" <<str5<<endl;

printf ("Resolution IP Address:");

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

(HOST_ENTRY->H_ADDR_LIST[0][0]&0X00FF),//Get the 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 ();

}

/* The program using the socket must call the WSAStartup function before using the socket.

The first parameter of the function indicates the socket version that the program requested to use,

Where the high byte indicates the secondary version, the low byte indicates the main version;

The operating system uses the second parameter to return the version information of the requested socket.

When an application calls the WSAStartup function, the operating system searches for the appropriate socket library based on the requested socket version.

Then bind the found socket library into the application.

The application can then invoke the other socket functions in the requested socket library.

Returns 0*/after successful execution of the function

Domain name resolved to IP address C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.