How to get the IP address of a machine

20 01 2009

 description11 

 

 

An Internet Protocol (IP) address is a unique identification number assigned to a machine participating in the Computer Network Communication. We could use ipconfig command in the Command Window/Prompt to get the IP address and some other Network Settings of our machine. Sometimes we need to get it in our code. right?

 howcanidoit18

 

 

We could use some Windows Socket APIs for this. Functions like WSAStartup &  gethostbyname and structures like sockaddr_in & hostent help us to accomplish this.

mycodesnippet7

 

 

#include "Winsock2.h"
void GetMyIPAddress()
{
   WSADATA WSAData;
  
   // Initialize winsock dll
   if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
   {
      AfxMessageBox(_T("Failed to find the WinSock DLL"));
      return;
   }
  
   char szHostName[128] = "";
  
   //get the standard host name of the machine
   if(::gethostname(szHostName, sizeof(szHostName)))
   {
      AfxMessageBox(_T("Failed to get the host name"));
   }
  
  
   struct sockaddr_in SocketAddress;
   struct hostent     *pHost        = 0;
  
   // Get local IP addresses
   pHost = ::gethostbyname(szHostName);
  
   if(!pHost)
   {
      AfxMessageBox(_T("Failed to get the host information."));
      return;
   }
  
   char aszIPAddresses[10][16]; // maximum of ten IP addresses
  
   for(int nCount = 0; ((pHost->h_addr_list[nCount]) && (nCount < 10)); ++nCount)
   {
      memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[nCount], pHost->h_length);
      strcpy(aszIPAddresses[nCount], inet_ntoa(SocketAddress.sin_addr));
   }
  
   // Cleanup
   WSACleanup();
}

mynote12

 

 

It is necessary to include the Ws2_32.lib library module with your application, otherwise you will experience with some link errors in the program.

Advertisements

Actions

Information

4 responses

22 05 2014
como hackear un facebook 2014

It is perfect time to make some plans for the future and it is time
to be happy. I’ve read this post and if I could I want to suggest you few interesting things or suggestions.
Perhaps you could write next articles referring to this article.
I want to read even more things about it!

31 07 2014
herbal face remedies

Hi there, There’s no doubt that your website might be having browser compatibility issues.

When I take a look at your site in Safari, it
looks fine but when opening in I.E., it’s got some overlapping issues.

I just wanted to give you a quick heads up! Apart
from that, great website!

17 09 2014
dgn2200 firmware custom iphone 3g

This is the right web site for anybody who really wants
to understand this topic. You understand so much its almost tough to argue with you (not that I really would want to…HaHa).

You certainly put a fresh spin on a topic that’s been written about for years.
Wonderful stuff, just excellent!

31 12 2016
garten und freizeit

Fabսlous, what а weblog it is! This web ѕite presents helpfᥙl data to us, keep it
up.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: