Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how can i dynamically get my machine ip address ?

how can i dynamically get my machine ip address ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marco M
    wrote on last edited by
    #1

    i need to get the ip of the machine my code is running on.. how can i program that ? thanx shakoosh

    D T R 3 Replies Last reply
    0
    • M Marco M

      i need to get the ip of the machine my code is running on.. how can i program that ? thanx shakoosh

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Call gethostname() followed by gethostbyname().


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      1 Reply Last reply
      0
      • M Marco M

        i need to get the ip of the machine my code is running on.. how can i program that ? thanx shakoosh

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        you can list all your IP Addresses by doing this :

        struct hostent* infohost;
        char HostName[256];
        char** addrs;
        char* Host;

        gethostname(HostName, 255);
        Host = HostName;
        infohost = gethostbyname(HostName);
        addrs = infohote -> h_addr_list;
        while (*addrs) {
        printf("IP : %s\n", inet_ntoa( *(struct in_addr *)* addrs));
        addrs++;
        }

        don't forget to check errors. Your main IP Address while being in a TCP network is in general the first, but not necessarily...


        TOXCCT >>> GEII power

        1 Reply Last reply
        0
        • M Marco M

          i need to get the ip of the machine my code is running on.. how can i program that ? thanx shakoosh

          R Offline
          R Offline
          RNEELY
          wrote on last edited by
          #4

          // GetMyManagedIP.cpp #include "stdafx.h" #using <mscorlib.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; int _tmain(int argc, _TCHAR* argv[]) { String* hostName = SystemInformation::ComputerName; System::Net::IPHostEntry* hostEntry = System::Net::Dns::GetHostByName(hostName); Console::WriteLine(S"Host name : {0}", hostEntry->HostName); for (System::Int32 i = 0; i < hostEntry->AddressList->Length; ++i) { Console::WriteLine(hostEntry->AddressList->Item[i]); } return 0; } Sincerely, -Ron

          R 1 Reply Last reply
          0
          • R RNEELY

            // GetMyManagedIP.cpp #include "stdafx.h" #using <mscorlib.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; int _tmain(int argc, _TCHAR* argv[]) { String* hostName = SystemInformation::ComputerName; System::Net::IPHostEntry* hostEntry = System::Net::Dns::GetHostByName(hostName); Console::WriteLine(S"Host name : {0}", hostEntry->HostName); for (System::Int32 i = 0; i < hostEntry->AddressList->Length; ++i) { Console::WriteLine(hostEntry->AddressList->Item[i]); } return 0; } Sincerely, -Ron

            R Offline
            R Offline
            RNEELY
            wrote on last edited by
            #5

            Or, if you don't like console output: // GetMyManagedIP.cpp #include "stdafx.h" #using <mscorlib.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; int _tmain(int argc, _TCHAR* argv[]) { #pragma push_macro("new") #undef new String* hostName = SystemInformation::ComputerName; System::Net::IPHostEntry* hostEntry = System::Net::Dns::GetHostByName(hostName); for (int i = 0; i < hostEntry->AddressList->Length; ++i) { Text::StringBuilder* sb = new Text::StringBuilder(); sb->Append(hostName); sb->AppendFormat(S" IP Address {0} = ", __box(i)); sb->Append(hostEntry->AddressList->Item[i]->ToString()); MessageBox::Show(sb->ToString()); } return 0; #pragma pop_macro("new") } Sincerely, -Ron

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups