For this ancient issue, the Essence of the VC Knowledge Base and the previous online magazines have been described in detail. The method provided herein is a relatively complete solution and accompanied by detailed implementation details. I hope everyone has a thorough understanding of this issue. In fact, if you are familiar with Windows Sockets API, and learn about some underlying Winsock knowledge. So what is not difficult to get the IP address of a machine. A machine can install multiple network cards, so it may have multiple IP addresses. At present, many PCs of the enthusiasts have multiple network cards. One of the NIC is connected to the modem or ADSL adapter, and the other is connected to the local area network (LAN) at home. This is a typical configuration for families with broadband connection conditions. Anything, once you know the solution, everything will become so simple. The following is a simple console program provided herein (program name getip1), which is the function of displaying the IP address of the unit. As shown in the figure:
Figure 1 Get1 running screen
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 machine's IP address.
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
// from Winsock.h
Struct 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 API 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 the intended map of the Hostent structure in memory:
Figure II Savory of HOSTENT Structure in Memory
I believe that the above explanation is supplemented to read the code, you will no longer feel unfamiliar with the Hostent structure.
Now .NET is popular, using C # language programming also a fashion, if anyone wants to know how to use C # to solve this article, please refer to the related content of the essence:
"
C # Program How to get the IP address of a machine? "
Finally, I will have a good programming!