C + + Complete domain name Resolution

Source: Internet
Author: User

Network class:
Customizing the Network.h header file

#ifndef _winsock_deprecated_no_warnings#define _winsock_deprecated_no_warnings#endif#ifndef _crt_secure_no_warnings#define _crt_secure_no_warnings#endif#ifndef _networkapi_#define _NETWORKAPI_#include <WinSock2.h>#pragma comment (lib, "Ws2_32.lib") class network{ Public: Network (); ~network ();protected:intErrChar*ipaddr[8];IP Address of the//storage domain name (multiple IP addresses may exist)    BOOLGethostip ();intTrue;//Mark valid IP address};#endif

Class Code implementation:

#include "network.h"#include <iostream>Network::~network () { for(inti =0; I <8; i++) {if(* (ipaddr + i))Delete[] * (ipaddr + i);Else             Break; }}network::network () {memset(IPAddr, NULL,8); Err = Gethostip ();}BOOLNetwork::gethostip () {//Load Socket fontWORD wversionrequested;    Wsadata Wsadata; wversionrequested = Makeword (1,1);//Initialize socket Dynamic Connection library, request 1.1 version of WinSocket libraryErr = WSAStartup (wversionrequested, &wsadata);if(Lobyte (wsadata.wversion)! =1||//Determine if the requested WinSocket version is 1.1Hibyte (wsadata.wversion)! =1) {WSACleanup ();//Liquidation        return false;//termination for use with WinSocket}//wsadata ws;     //wsastartup (Makeword (2,2), &WS);//    CharHttp[] ="Www.myvip6.com";//Access server domain nameSocket sock = socket (af_inet, Sock_stream,0);//Build Socket    if(sock = = Invalid_socket) {STD::cout<<"Build access socket socket failed!"<<STD:: Endl;return false; } hostent* host = gethostbyname (HTTP);//Get the IP address of the host    if(host = = NULL) {//std::cout << "host is not connected;" << Std::endl;        return false; } in_addr addr; for(inti =0;; i++) {Char*p = host->h_addr_list[i];if(p = = NULL) Break;memcpy(&addr. S_un.        S_ADDR, p, host->h_length); * (ipaddr + i) =New Char[strlen(Inet_ntoa (addr)) +1];strcpy(* (ipaddr + i), Inet_ntoa (addr));//std::cout << * (ipaddr + i);}return true;}

Complete domain name resolution via socket

C + + Complete domain name Resolution

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.