How to use C + + programming to obtain the IP address of a machine

Source: Internet
Author: User
Tags printf knowledge base

This article supporting source code

The essence of the VC knowledge Base and the previous online magazines are described in detail in this age-old problem. The approach provided in this article is a more complete solution with detailed implementation details. I hope you have a thorough understanding of this issue. In fact, if you are familiar with Windows sockets API, and understand some of the underlying Winsock knowledge. So it's not difficult to get the IP address of a machine. A machine can have more than one NIC, so it may have multiple IP addresses. At present many enthusiasts are equipped with many PC network card. One of the network cards is connected to the modem or the ADSL adapter, and the other is connected to the home local area network (LAN). For families with broadband connectivity, this is a typical configuration. Anything, once you know the solution, everything will be so simple. The following is a simple console program (program named GETIP1) provided in this article that displays the IP address of this computer. As shown in Figure one:

Figure one getip1 the running picture

The following is the code for the GETIP1 program, very simple: ////////////////////////////////////////////////////////////////
//Getip1.cpp
//
//This program reports the IP address of each network card on this machine
///Command Line compilation command is:
//
//CL getip1.cpp wsock32.lib
//
//Please be sure to specify LIB in the environment variable correctly The path to the library; you can run Vcvars32.bat
//
#include <winsock.h>
#include <wsipx.h>
#include <wsnwlink.h
#include <stdio.h>
int main ()
{
////////////////
//Initialize the Windows sockets API. Request version for Vers   Ion 1.1
//
WORD wversionrequested = Makeword (1, 1);
Wsadata Wsadata;    
if (WSAStartup (wversionrequested, &wsadata)) {
printf ("WSAStartup failed%s\n", WSAGetLastError ());
return-1;  
}
//////////////////
//Get 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);
 
Gets the host information based on the host name.  
//
hostent* phostent = gethostbyname (hostname);
   if (phostent==null) {
printf ("Error:%u\n", WSAGetLastError ());
return-1;  
}
//////////////////
//resolves the hostent information returned.

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[nadap Ter],he.h_length);
//Output The IP address of the machine.
printf ("Address:%s\n", Inet_ntoa (SA.SIN_ADDR));//Show addresses string

//////////////////
//Terminate Windows Soc   Kets API
//
WSACleanup ();
return 0;
}

Related Article

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.