[转] How to use C ++ to obtain an IP address of a machine

xiaoxiao2021-03-06  40

Below is the code of the getIP1 program, very simple:

// Getip1.cpp

//

/ / This program reports the IP address of each network card on this unit

// Command line compile command is:

//

// Cl Getip1.cpp wsock32.lib

//

/ / Please specify the path to the lib library in the environment variable; you can run vcvars32.bat

//

#include

#include

#include

#include

int main ()

{

// Initialize the Windows Sockets API. Requirements Version 1.1

//

Word wversionRequested = MakeWord (1, 1);

Wsadata wsadata;

IF (WSAStartup (WVersionRequested, & WSADATA) {

Printf ("WSAStartup Failed% S / N", Wsagetlasterror ());

Return -1;

}

//

// Get the host name.

//

Char Hostname [256];

Int res = gethostname (Hostname, SizeOf (Hostname));

IF (res! = 0) {

Printf ("error:% u / n", wsagetlasterror ());

Return -1;

}

Printf ("Hostname =% S / N", HostName);

/ / Get the host information according to the host name.

//

Hostent * phostent = gethostByName (Hostname);

IF (phostent == NULL) {

Printf ("error:% u / n", wsagetlasterror ());

Return -1;

}

//

// Analyze the returned HOSTENT information.

//

Hostent & he = * phostent;

Printf ("name =% s / naaliases =% s / naddrtype =% D / NLENGTH =% D / N",

HE.H_NAME, He.H_Aliases, He.H_Addrtype, He.h_length

SockAddr_in SA;

For (int Nadapter = 0; he.h_addr_list [nadapter]; NADAPTER ) {

Memcpy (& sa.sin_addr.s_addr, he.h_addr_list [nadapter], he.h_length);

/ / Output the IP address of the machine.

Printf ("Address:% S / N", INET_NTOA (Sa.sin_ADDR)); // Displays address string

}

//

// Terminate Windows Sockets API

//

WSACLEANUP ();

Return 0;

} To use Winsock, you must first call WSAStartup. Don't forget to call WSACleanup at the end. To get an IP address, you must first get the host name (Host Name), calling gethostname, you can implement, with the host name, next to call gethostByName to get more host information including the IP address. GethostByname returns a pointer to the Hostent data structure, which is defined in the file:

// from Winsock.hstruct Hostent {

Char far * h_name; / * official host name * /

Char Far * Far * h_aliases; / * Alias ​​list * /

Short h_addrtype; / * Host address type * /

Short h_length; / * address length * /

Char Far * Far * h_addr_list; / * Address List * /

}

This is a typical underlying APIS used data structure, many people are not very familiar with it. In fact, Hostent is a growing data structure, h_name is the host name, and the value in the example program is "zxn.hangwire.sdb". There is no alias (h_aliases). h_addrtype is the address type (or also called an address family), in the example of the value of 2 (AF_INET = Internet, other content see Winsock.h). H_length is the length of each address, in bytes. Since the length of the IP address is 4 bytes, the value in the example program is 4, h_addr_list is the beginning point of the address array, and one is stored, the end is a NULL. Each x.y.z.w digit accounts for one byte. In order to format the IP address into X.y.z.w, you must copy the address number first to a data structure called SockAddr, and then call a special function INT_NTOA. Figure 2 is a map of the Hostent structure in memory: Figure two Hostent structure stores in memory

转载请注明原文地址:https://www.9cbs.com/read-56313.html

New Post(0)