Menu

[dev-cpp] Get IP on WinXP/2003

Juzbrig
2009-01-17
2012-09-26
  • Juzbrig

    Juzbrig - 2009-01-17

    Hi
    Could someone show me a simple method for getting an IP address (working under dev-cpp + WinXp and WinServ2003) ? I need only a default address (one showed with ipconfig command). What do I have to include ?

     
    • cpns

      cpns - 2009-01-17

      I Googled "win32 get local ip address" which yielded a number of suitable answers (including the top entry): http://windows-programming.suite101.com/article.cfm/socket_programming_gethostbyname

      The trick is to use gethostname() to get the local host name, then use that with gethostbyname() to get hostent structure which enumerates all network adapters present. The default one being at host_entry.h_addr_list[0].

      Clifford

       
    • cpns

      cpns - 2009-01-17

      From the second link in the Google search an example of exactly what I said:

      // Init WinSock
      WSADATA wsa_Data;
      int wsa_ReturnCode = WSAStartup(0x101,&wsa_Data);

      // Get the local hostname
      char szHostName[255];
      gethostname(szHostName, 255);
      struct hostent host_entry;
      host_entry=gethostbyname(szHostName);
      char * szLocalIP;
      szLocalIP = inet_ntoa (
      (struct in_addr )host_entry->h_addr_list);
      WSACleanup();

      Note that you might consider using Stackoverflow for non Dev-C++ specific problems in the future. It is rapidly becoming the place for programming discussion, and is far more innovative and dare I say it "Web 2.0" than most forums (and especially Sourceforge which is in the dark ages by comparison!).

      Clifford

       
    • Juzbrig

      Juzbrig - 2009-01-17

      Thnx, it works

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.