Advertisement

How to get local ip address in string form?

Started by August 15, 2004 11:29 AM
0 comments, last by Nerusai 20 years, 6 months ago
Hi, in wondering how to get he local ip address. I was able to get the computer name like so:
Quote:
char hostname[50]; gethostname(hostname,50);
But i can't work out how to get the ip thanks
Sigh, you people really need to look a bit before you go asking around.

From the MS knowledge base:

#include <windows.h>   #include <winsock.h>   #include <stdio.h>   void main()   {      WSADATA  wsaData;      char     szHostname[100];      HOSTENT *pHostEnt;      int      nAdapter = 0;      struct   sockaddr_in sAddr;      if (WSAStartup(0x0101, &wsaData))      {         printf("WSAStartup failed %s\n", WSAGetLastError());         return;      }      gethostname( szHostname, sizeof( szHostname ));      pHostEnt = gethostbyname( szHostname );      while ( pHostEnt->h_addr_list[nAdapter] )      {       // pHostEnt->h_addr_list[nAdapter] is the current address in host       // order.       // Copy the address information from the pHostEnt to a sockaddr_in       // structure.         memcpy ( &sAddr.sin_addr.s_addr, pHostEnt->h_addr_list[nAdapter],                  pHostEnt->h_length);         // Output the machines IP Address.         printf("Name:    %s\nAddress: %s\n", pHostEnt->h_name,                inet_ntoa(sAddr.sin_addr));         nAdapter++;      }      WSACleanup();      return;   }


Or Click Me!(Link into MS Knowledge Base)
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.

This topic is closed to new replies.

Advertisement