How can I use C ++ to obtain the IP address of a machine?
Compilation: northtibet
Download source code
For this old question, the essence of the VC knowledge base and previous online magazines have different details. The method provided in this article is a complete solution with detailed implementation details. I hope you will have a thorough understanding of this issue. In fact, if you are familiar with the Windows Sockets API and learn some underlying Winsock knowledge. It is not difficult to obtain the IP address of a machine. A machine can have multiple NICs, so it may have multiple IP addresses. Currently, many enthusiasts have multiple NICs on their PCs. One network adapter is connected to the modem or ADSL adapter, and the other is connected to the LAN at home. For families with broadband connection conditions, this is a typical configuration. Everything becomes so simple once you know the solution. The following is a simple console program (the program name is getip1) provided in this Article. Its function is to display the IP address of the local machine. 1:
Figure 1 running figure of getip1
The following is the code of the getip1 program, which is simple:
//////////////////////////////////////// //////////////////////// Getip1.cpp //// this program reports IP address // command line compiling command: /// Cl getip1.cpp wsock32.lib // you must specify the Lib library path in the environment variable. You can run vcvars32.bat // # include <Winsock. h> # include <wsipx. h> # include <wsnwlink. h> # include <stdio. h> int main () {// initialize the Windows Sockets API. required version: Version 1.1 // word wversionrequested = makeword (1, 1); wsadata; I F (wsastartup (wversionrequested, & wsadata) {printf ("wsastartup failed % s/n", wsagetlasterror (); Return-1 ;} /// // obtain 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 ); //// // obtain the host information based on the host name. // hostent * phostent = gethostbyname (hostname); If (phostent = NULL) {printf ("error: % u/N", wsagetlasterror (); Return-1 ;} /////////////////// parse the returned hostent information. // hostent & He = * phostent; printf ("name = % S/naliases = % 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); // the IP address of the output machine. printf ("Address: % s/n", inet_ntoa (SA. sin_addr); // display address string} // terminate the Windows Sockets API // wsacleanup (); return 0 ;}
To Use WinSock, you must first call wsastartup. Do not forget to call wsacleanup at the end. To obtain an IP address, you must first obtain the Host Name of the machine. Call gethostname to obtain the host name. With the host name, call gethostbyname to obtain more host information, including the IP address. Gethostbyname returns a pointer to the hostent data structure, which is defined in the <Winsock. h> file as follows:
// From Winsock. hstruct hostent {char far * h_name;/* official host name */Char far * h_aliases;/* alias list */short h_addrtype; /* Host address type */short h_length;/* address length */Char far * h_addr_list;/* address list */};
This is a typical data structure used by the underlying APIs. Many people are not familiar with it. In fact, the hostent is a variable-length data structure, h_name is the host name, and the value in the example program is "zxn. hangwire. SDB ". No alias (h_aliases ). H_addrtype is an address type (or an address family). The value in the example program is 2 (af_inet = Internet. For more information, see Winsock. h ). H_length is the length of each address, in bytes. Because the IP address length is 4 bytes, the value in the example program is 4. h_addr_list is the starting point of the address array. They are stored one by one, and the end is null. Each x. y. Z. W number occupies one byte. To format the IP address as x. y. Z. W, you must copy the address array to a sockaddr data structure and then call a special function inet_ntoa. Figure 2 shows that the hostent structure is stored in the memory:
Figure 2 storage of the hostent structure in memory
I believe that the above explanation is supplemented by reading the code, and you will no longer be unfamiliar with the hostent structure in the future.
Currently. NET is popular, and C # programming has become a fashion. If anyone wants to know how to use C # to solve the problem in this article, please refer to the content of the essence area:
"C # How do I obtain the IP address of a machine ?"
Finally, we wish you a pleasant programming!
From http://www.vckbase.com/document/viewdoc? Id = 435