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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get IP address?

How to get IP address?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorialworkspace
12 Posts 4 Posters 0 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
    monsieur_jj
    wrote on last edited by
    #1

    Hi all How do I get my IP address by code? Whats the shortest way possible? I tried getenv but I found out not every computer has their IP add in their environment variables. Thanks, Jayjay

    K H 2 Replies Last reply
    0
    • M monsieur_jj

      Hi all How do I get my IP address by code? Whats the shortest way possible? I tried getenv but I found out not every computer has their IP add in their environment variables. Thanks, Jayjay

      K Offline
      K Offline
      kcynic
      wrote on last edited by
      #2

      get host name first and then get the details. The following code might be helpful: char host_name[128]; hostent* host; gethostname(host_name,128); host = gethostbyname(host_name); printf("The local host: Address : %s\n", inet_ntoa(*(struct in_addr*)host->h_addr)); Regards

      M 1 Reply Last reply
      0
      • K kcynic

        get host name first and then get the details. The following code might be helpful: char host_name[128]; hostent* host; gethostname(host_name,128); host = gethostbyname(host_name); printf("The local host: Address : %s\n", inet_ntoa(*(struct in_addr*)host->h_addr)); Regards

        M Offline
        M Offline
        monsieur_jj
        wrote on last edited by
        #3

        Hi kcynic, the gethostname didnt fill up the host_name. Is there a requirement before using your code? Thanks, Jayjay

        K 1 Reply Last reply
        0
        • M monsieur_jj

          Hi kcynic, the gethostname didnt fill up the host_name. Is there a requirement before using your code? Thanks, Jayjay

          K Offline
          K Offline
          kcynic
          wrote on last edited by
          #4

          did you forget to call WSAStartup?

          M 1 Reply Last reply
          0
          • K kcynic

            did you forget to call WSAStartup?

            M Offline
            M Offline
            monsieur_jj
            wrote on last edited by
            #5

            Hi kcynic, It is working now however there is no h_addr just h_addr_list[0] in the struct of hostent, I still can't find the ipadd. Whats your advise on this? Thanks, Jayjay

            enhzflepE K 2 Replies Last reply
            0
            • M monsieur_jj

              Hi kcynic, It is working now however there is no h_addr just h_addr_list[0] in the struct of hostent, I still can't find the ipadd. Whats your advise on this? Thanks, Jayjay

              enhzflepE Offline
              enhzflepE Offline
              enhzflep
              wrote on last edited by
              #6

              Hmm, interesting. With the following code compiled and linked with wsock32, I get an output of Hostname: xpDesktop The local host: Address : 210.84.23.xxx Press any key to continue . . .

              #include <windows.h>
              #include <winsock2.h>
              #include <stdio.h>

              int main()
              {
              char host_name[128];
              hostent* host;
              WORD verRequested = MAKEWORD(1,1), err; // version 1.1
              WSADATA wsaData;
              err = WSAStartup(verRequested, &wsaData);
              gethostname(host_name,128);
              printf("Hostname: %s\n", host_name);
              host = gethostbyname(host_name);
              printf("The local host: Address : %s\n", inet_ntoa(*(struct in_addr*)host->h_addr));
              WSACleanup();
              system("pause");
              }

              1 Reply Last reply
              0
              • M monsieur_jj

                Hi kcynic, It is working now however there is no h_addr just h_addr_list[0] in the struct of hostent, I still can't find the ipadd. Whats your advise on this? Thanks, Jayjay

                K Offline
                K Offline
                kcynic
                wrote on last edited by
                #7

                in winsock2.h: struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias list */ short h_addrtype; /* host address type */ short h_length; /* length of address */ char FAR * FAR * h_addr_list; /* list of addresses */ #define h_addr h_addr_list[0] /* address, for backward compat */ }; if you call WSAStartup first, you should get the result.

                M 1 Reply Last reply
                0
                • K kcynic

                  in winsock2.h: struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias list */ short h_addrtype; /* host address type */ short h_length; /* length of address */ char FAR * FAR * h_addr_list; /* list of addresses */ #define h_addr h_addr_list[0] /* address, for backward compat */ }; if you call WSAStartup first, you should get the result.

                  M Offline
                  M Offline
                  monsieur_jj
                  wrote on last edited by
                  #8

                  Actually yes i called WSAStartup first The host name is jdeguzman however the h_addr_list[0] value is

                  *jdeguzman

                  which i dont get why it returns that instead my ip address. Thanks, Jayjay

                  K 1 Reply Last reply
                  0
                  • M monsieur_jj

                    Actually yes i called WSAStartup first The host name is jdeguzman however the h_addr_list[0] value is

                    *jdeguzman

                    which i dont get why it returns that instead my ip address. Thanks, Jayjay

                    K Offline
                    K Offline
                    kcynic
                    wrote on last edited by
                    #9

                    yes, my result is similar to yours. and the ip result is returned by calling inet_ntoa function. what's you code look like? of course you should check all the callings successful or not. msdn will give you more details i think.

                    M 1 Reply Last reply
                    0
                    • K kcynic

                      yes, my result is similar to yours. and the ip result is returned by calling inet_ntoa function. what's you code look like? of course you should check all the callings successful or not. msdn will give you more details i think.

                      M Offline
                      M Offline
                      monsieur_jj
                      wrote on last edited by
                      #10

                      Its all working now Thanks!

                      1 Reply Last reply
                      0
                      • M monsieur_jj

                        Hi all How do I get my IP address by code? Whats the shortest way possible? I tried getenv but I found out not every computer has their IP add in their environment variables. Thanks, Jayjay

                        H Offline
                        H Offline
                        Hamid Taebi
                        wrote on last edited by
                        #11

                        And see Getting Addresses IP informations[^] of course its more of your answer but it has good info for you. :)

                        M 1 Reply Last reply
                        0
                        • H Hamid Taebi

                          And see Getting Addresses IP informations[^] of course its more of your answer but it has good info for you. :)

                          M Offline
                          M Offline
                          monsieur_jj
                          wrote on last edited by
                          #12

                          This is my function for you all:

                          char* GetIpAddress()
                          {
                          WSADATA wsaData;

                          	char host\_name\[128\];    
                          	hostent\* host;    
                          	WORD verRequested = MAKEWORD(1,1), err;  
                          	err = WSAStartup(verRequested, &wsaData);    
                          	gethostname(host\_name,128);      
                          	host = gethostbyname(host\_name);   
                          	host->h\_addr\_list\[0\];  
                          	WSACleanup();    
                          	
                          	return inet\_ntoa(\*(struct in\_addr\*)host->h\_addr);
                          

                          }

                          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