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