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. IP Address of own PC

IP Address of own PC

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 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.
  • R R Thomas 0

    nope...doesnt work!!!:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

    J Offline
    J Offline
    John M Drescher
    wrote on last edited by
    #4

    Try initializing winsock first:

    #include "Winsock2.h"
    #pragma comment(lib,"Ws2_32.lib")

    int main(int argc, char* argv[])
    {

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 2, 2 );

    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL. */
    return -1;
    }

    char host\[80\];
    gethostname(host,80);
    LPHOSTENT lpHost;
    lpHost = gethostbyname(host);
    UINT ip = ((LPIN\_ADDR)lpHost->h\_addr)->s\_addr;
    
        char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr);
    
    return 0;
    

    }

    John

    R 1 Reply Last reply
    0
    • J John M Drescher

      Try initializing winsock first:

      #include "Winsock2.h"
      #pragma comment(lib,"Ws2_32.lib")

      int main(int argc, char* argv[])
      {

      WORD wVersionRequested;
      WSADATA wsaData;
      int err;

      wVersionRequested = MAKEWORD( 2, 2 );

      err = WSAStartup( wVersionRequested, &wsaData );
      if ( err != 0 ) {
      /* Tell the user that we could not find a usable */
      /* WinSock DLL. */
      return -1;
      }

      char host\[80\];
      gethostname(host,80);
      LPHOSTENT lpHost;
      lpHost = gethostbyname(host);
      UINT ip = ((LPIN\_ADDR)lpHost->h\_addr)->s\_addr;
      
          char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr);
      
      return 0;
      

      }

      John

      R Offline
      R Offline
      R Thomas 0
      wrote on last edited by
      #5

      Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Autoconfiguration IP Address. . . : 169.254.159.128 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter PacNet: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 210.24.247.6 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 210.24.247.6 the codes u gave me are fetching Autoconfiguration IP Address i.e 169.254.159.128 what i need is IP Address i.e 210.24.247.6 can someone please help???:(( With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

      J 2 Replies Last reply
      0
      • R R Thomas 0

        Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Autoconfiguration IP Address. . . : 169.254.159.128 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter PacNet: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 210.24.247.6 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 210.24.247.6 the codes u gave me are fetching Autoconfiguration IP Address i.e 169.254.159.128 what i need is IP Address i.e 210.24.247.6 can someone please help???:(( With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #6

        Joseph_R_Thomas wrote: can someone please help??? The problem is you have more than one ip address. The code above will return the first. replace h_addr in the previous examples with h_addr_list[1] to get the second. John

        1 Reply Last reply
        0
        • R R Thomas 0

          Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Autoconfiguration IP Address. . . : 169.254.159.128 Subnet Mask . . . . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . . . . : PPP adapter PacNet: Connection-specific DNS Suffix . : IP Address. . . . . . . . . . . . : 210.24.247.6 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 210.24.247.6 the codes u gave me are fetching Autoconfiguration IP Address i.e 169.254.159.128 what i need is IP Address i.e 210.24.247.6 can someone please help???:(( With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #7

          See the docs for hostent[^] and gethostbyname[^] for a better understanding of what is going on. John

          R 1 Reply Last reply
          0
          • J John M Drescher

            See the docs for hostent[^] and gethostbyname[^] for a better understanding of what is going on. John

            R Offline
            R Offline
            R Thomas 0
            wrote on last edited by
            #8

            :confused::confused::~ but what if the program keeps on shifting from one machine to another where some machine have more than one IP addresses and soem have only one????:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

            J 1 Reply Last reply
            0
            • R R Thomas 0

              :confused::confused::~ but what if the program keeps on shifting from one machine to another where some machine have more than one IP addresses and soem have only one????:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

              J Offline
              J Offline
              John M Drescher
              wrote on last edited by
              #9

              It gets very complicated. Any network adapter may have multiple IP addresses. A computer may have more than one network adapters. I am not sure how you can figure out which ip address is the one that is connected to the internet and actually more than one can be connected to the internet. You can use this code to go through the list of ip addresses:

              #include "Winsock2.h"
              #pragma comment(lib,"Ws2_32.lib")

              int main(int argc, char* argv[])
              {

              WORD wVersionRequested;
              WSADATA wsaData;
              int err;

              wVersionRequested = MAKEWORD( 2, 2 );

              err = WSAStartup( wVersionRequested, &wsaData );
              if ( err != 0 ) {
              /* Tell the user that we could not find a usable */
              /* WinSock DLL. */
              return -1;
              }

              char host\[80\];
              gethostname(host,80);
              LPHOSTENT lpHost;
              lpHost = gethostbyname(host);
              
              
              for(int i=0;(LPIN\_ADDR)lpHost->h\_addr\_list\[i\] != 0;i++) {
              	UINT ip = ((LPIN\_ADDR)lpHost->h\_addr\_list\[i\])->s\_addr;
              	char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr\_list\[i\]);
              }
              
              return 0;
              

              }

              John

              R 1 Reply Last reply
              0
              • J John M Drescher

                It gets very complicated. Any network adapter may have multiple IP addresses. A computer may have more than one network adapters. I am not sure how you can figure out which ip address is the one that is connected to the internet and actually more than one can be connected to the internet. You can use this code to go through the list of ip addresses:

                #include "Winsock2.h"
                #pragma comment(lib,"Ws2_32.lib")

                int main(int argc, char* argv[])
                {

                WORD wVersionRequested;
                WSADATA wsaData;
                int err;

                wVersionRequested = MAKEWORD( 2, 2 );

                err = WSAStartup( wVersionRequested, &wsaData );
                if ( err != 0 ) {
                /* Tell the user that we could not find a usable */
                /* WinSock DLL. */
                return -1;
                }

                char host\[80\];
                gethostname(host,80);
                LPHOSTENT lpHost;
                lpHost = gethostbyname(host);
                
                
                for(int i=0;(LPIN\_ADDR)lpHost->h\_addr\_list\[i\] != 0;i++) {
                	UINT ip = ((LPIN\_ADDR)lpHost->h\_addr\_list\[i\])->s\_addr;
                	char\* addr= inet\_ntoa(\*(LPIN\_ADDR)lpHost->h\_addr\_list\[i\]);
                }
                
                return 0;
                

                }

                John

                R Offline
                R Offline
                R Thomas 0
                wrote on last edited by
                #10

                but then how to select the correct one????:~ :sigh::confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

                J 1 Reply Last reply
                0
                • R R Thomas 0

                  but then how to select the correct one????:~ :sigh::confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

                  J Offline
                  J Offline
                  John M Drescher
                  wrote on last edited by
                  #11

                  Joseph_R_Thomas wrote: but then how to select the correct one???? They are all correct. I will explain. Think about the case where there is a lan connected to one network adapter and a second network adapter is conected to the internet using a dsl modem. One ip adddress will work to talk to the other pcs on the lan. The other ip address will work for the internet. I assume you mean the one connected to the internet?? I am not sure. John

                  R 1 Reply Last reply
                  0
                  • J John M Drescher

                    Joseph_R_Thomas wrote: but then how to select the correct one???? They are all correct. I will explain. Think about the case where there is a lan connected to one network adapter and a second network adapter is conected to the internet using a dsl modem. One ip adddress will work to talk to the other pcs on the lan. The other ip address will work for the internet. I assume you mean the one connected to the internet?? I am not sure. John

                    R Offline
                    R Offline
                    R Thomas 0
                    wrote on last edited by
                    #12

                    u see..in my school its al connected to lan so there is only one ip.. at my house however as i showed you in the first or second post..there are more than one IP Address....in this case i need the IP Address that connects to the internet...the one that is called IP Address not AutoConfiguration IP Address..... so in school the codes work fine..but at home...i got prob since i need to select the IP Address not the Auto config IP Address....:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

                    P 1 Reply Last reply
                    0
                    • R R Thomas 0

                      u see..in my school its al connected to lan so there is only one ip.. at my house however as i showed you in the first or second post..there are more than one IP Address....in this case i need the IP Address that connects to the internet...the one that is called IP Address not AutoConfiguration IP Address..... so in school the codes work fine..but at home...i got prob since i need to select the IP Address not the Auto config IP Address....:confused: With Regards Joseph R. Thomas Have a Super Blessed Day! ------------------------- For God has not given us a spirit of fear, but of power and of love and of a sound mind. 2 Timothy 1:7

                      P Offline
                      P Offline
                      Phil Speller
                      wrote on last edited by
                      #13

                      I might be on the wrong track here but when you call bind on a socket you can specify ADDR_ANY for the ip address. Then, windows will sort out which actual IP address to use based on routing information - this saves you having to manually determine which IP address you need to select. Phil

                      J 1 Reply Last reply
                      0
                      • P Phil Speller

                        I might be on the wrong track here but when you call bind on a socket you can specify ADDR_ANY for the ip address. Then, windows will sort out which actual IP address to use based on routing information - this saves you having to manually determine which IP address you need to select. Phil

                        J Offline
                        J Offline
                        John M Drescher
                        wrote on last edited by
                        #14

                        I was thinking the same. It depends why he wants the address. John

                        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