Advertisement

Help with getting ip

Started by February 06, 2005 06:03 PM
5 comments, last by bjogio 19 years, 11 months ago
How do I figure out the ip of my system. It changes constantly and I need to know because I am testing a server on my computer. When I run the client on the same computer I can just connect to 127.000.000.001, but when on different computers it never works.
Mike Popoloski | Journal | SlimDX
If you want to do it programatically you can use something like this (Modeled fron CodeGuru):

#include <winsock2.h>#include <stdio.h>#pragma comment (lib,"Wsock32.lib")void DisplayIP(){      WORD wVersionRequested;      WSADATA wsaData;      char name[255];      char ip[256];      PHOSTENT hostinfo;      wVersionRequested = MAKEWORD( 2, 0 );      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )      {            if( gethostname ( name, sizeof(name)) == 0)            {                  if((hostinfo = gethostbyname(name)) != NULL)                  {                        sprintf(ip,inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list) );                  }            }            WSACleanup( );      }      printf( ip ); // or do whatever you want to do with it, assuming it was retrieved}int main( int argc, char* argv[] ){	DisplayIP();	return 0;}


Hope this helps.

- Drew
Advertisement
Calling gethostname() and then gethostbyname() may not always actually return the local IP, depending on how your ISP has set up the reverse DNS mapping (or, if they have done it at all). It may also not work right when you're beind a NAT (because it's unlikely there's proper reverse DNS there).

On some systems, you can create a UDP socket, bind it to INADDR_ANY, and then call getsockname() to get the address of the first local interface. However, many systems "helpfully" return INADDR_ANY in this case, so I wouldn't base production code on it.

To get all the local IP addresses in a reliable way (you may have more than one), you have to use system-specific APIs to enumerate all configured network adapters, which means walking through /proc/net on Linux, and using the device manager APIs on Windows. However, that will still only get you a local address, which may or may not be useful for someone outside your local subnet.

To get the publicly visible IP of your server, if you're hosting from beind a firewall using port forwarding, or NAT punch-through, you have to ask the user, or ask some external server with a known name/address (known as an introducer).
enum Bool { True, False, FileNotFound };
here: http://programmer.altervista.org/Data/ip.exe
there is a little program I wrote years ago that when double-clicked copy your ip in the clipboard. if you want source (20 lines) tell me.
[ILTUOMONDOFUTURO]
Can you post that source, I would be intrested inseing it.
www.stickskate.com -> check it out, some gnarly stick skating movies
*DIG*
#include "windows.h"#include <winsock.h>void copytoclipboard(char* what){    char* lptstrCopy;     HGLOBAL hglbCopy;    if (!OpenClipboard(NULL))         return;    EmptyClipboard(); 	hglbCopy = GlobalAlloc(GMEM_MOVEABLE, strlen(what)+1);         	if (hglbCopy == NULL) {		CloseClipboard(); 		return; } 	lptstrCopy = (char*)GlobalLock(hglbCopy); 	memcpy(lptstrCopy, what, strlen(what)+1); 	GlobalUnlock(hglbCopy); 	SetClipboardData(CF_TEXT, hglbCopy);     CloseClipboard(); }int doit(){    char ac[80];    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {		MessageBox(NULL, "uh ah", "eh", NULL);        return 1;    }    struct hostent *phe = gethostbyname(ac);    if (phe == 0) {        MessageBox(NULL, "uh oh", "uh", NULL);        return 1;    }    for (int i = 0; phe->h_addr_list != 0; ++i) {        struct in_addr addr;        memcpy(&addr, phe->h_addr_list, sizeof(struct in_addr));		copytoclipboard(inet_ntoa(addr));    }    return 0;}int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){    WSAData wsaData;    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)        return 255;    int retval = doit();    WSACleanup();    return retval;}
[ILTUOMONDOFUTURO]

This topic is closed to new replies.

Advertisement