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. Get IP address of computer

Get IP address of computer

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
17 Posts 3 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.
  • W Offline
    W Offline
    waxie
    wrote on last edited by
    #1

    Hi gurus, How do we get the ip address of computer using MFC? Any clues? Ideas? :rose:Thanks, Wax

    L 1 Reply Last reply
    0
    • W waxie

      Hi gurus, How do we get the ip address of computer using MFC? Any clues? Ideas? :rose:Thanks, Wax

      L Offline
      L Offline
      Laxman9
      wrote on last edited by
      #2

      See the following from which u can get the IP address of the local machine gethostname(szHostName, strlen( szHostName ) ); //returns machine name strFullName="\\\\"+szHostName; host = gethostbyname(strFullName); //returns hostent* if(host!=NULL) { ptr = (struct in_addr *) host->h_addr_list[0]; int a = ptr->S_un.S_un_b.s_b1; // Eg. 211.40.35.76 split up like this. int b = ptr->S_un.S_un_b.s_b2; // 40 int c = ptr->S_un.S_un_b.s_b3; // 35 int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%d.%d.%d.%d",a,b,c,d); AfxMessageBox("IP:="+strTemp); } Thanks and Regards Laxman :cool: FAILURE is the first step towards SUCCESS :cool: -- modified at 23:45 Friday 20th January, 2006

      O 1 Reply Last reply
      0
      • L Laxman9

        See the following from which u can get the IP address of the local machine gethostname(szHostName, strlen( szHostName ) ); //returns machine name strFullName="\\\\"+szHostName; host = gethostbyname(strFullName); //returns hostent* if(host!=NULL) { ptr = (struct in_addr *) host->h_addr_list[0]; int a = ptr->S_un.S_un_b.s_b1; // Eg. 211.40.35.76 split up like this. int b = ptr->S_un.S_un_b.s_b2; // 40 int c = ptr->S_un.S_un_b.s_b3; // 35 int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%d.%d.%d.%d",a,b,c,d); AfxMessageBox("IP:="+strTemp); } Thanks and Regards Laxman :cool: FAILURE is the first step towards SUCCESS :cool: -- modified at 23:45 Friday 20th January, 2006

        O Offline
        O Offline
        Owner drawn
        wrote on last edited by
        #3

        Is this MFC?:omg:

        Jesus Loves:rose:

        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

        W 1 Reply Last reply
        0
        • O Owner drawn

          Is this MFC?:omg:

          Jesus Loves:rose:

          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

          W Offline
          W Offline
          waxie
          wrote on last edited by
          #4

          Yes, MFC. I'm not using .NET. just pure visual c++. waxie

          O 1 Reply Last reply
          0
          • W waxie

            Yes, MFC. I'm not using .NET. just pure visual c++. waxie

            O Offline
            O Offline
            Owner drawn
            wrote on last edited by
            #5

            I was asking whether the code given by Laxman is MFC or not. It's SDK.:^)

            Jesus Loves:rose:

            --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

            W 1 Reply Last reply
            0
            • O Owner drawn

              I was asking whether the code given by Laxman is MFC or not. It's SDK.:^)

              Jesus Loves:rose:

              --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

              W Offline
              W Offline
              waxie
              wrote on last edited by
              #6

              Yeah. It's in .NET. I just want to retrieve IP address and then store it in a string. waxie

              O 1 Reply Last reply
              0
              • W waxie

                Yeah. It's in .NET. I just want to retrieve IP address and then store it in a string. waxie

                O Offline
                O Offline
                Owner drawn
                wrote on last edited by
                #7

                waxie wrote:

                It's in .NET

                It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.

                Jesus Loves:rose:

                --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                W 2 Replies Last reply
                0
                • O Owner drawn

                  waxie wrote:

                  It's in .NET

                  It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.

                  Jesus Loves:rose:

                  --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                  W Offline
                  W Offline
                  waxie
                  wrote on last edited by
                  #8

                  I've tried using the code but it still does not work. I'm making some tweaks. Hopefully this will work. waxie

                  1 Reply Last reply
                  0
                  • O Owner drawn

                    waxie wrote:

                    It's in .NET

                    It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.

                    Jesus Loves:rose:

                    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                    W Offline
                    W Offline
                    waxie
                    wrote on last edited by
                    #9

                    I'm getting these errors when linking: error LNK2001: unresolved external symbol __imp__gethostbyname@4 error LNK2001: unresolved external symbol __imp__gethostname@8 you have any idea what causes these errors? I guess the linker is looking for a lib file or something.. waxie

                    O 1 Reply Last reply
                    0
                    • W waxie

                      I'm getting these errors when linking: error LNK2001: unresolved external symbol __imp__gethostbyname@4 error LNK2001: unresolved external symbol __imp__gethostname@8 you have any idea what causes these errors? I guess the linker is looking for a lib file or something.. waxie

                      O Offline
                      O Offline
                      Owner drawn
                      wrote on last edited by
                      #10

                      Have you added support for WinSock in your project. This happens if you don't have support for winsock in your project. You need to include Winsock2.h And which version of Visual Studio are you using.

                      Jesus Loves:rose:

                      --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                      W 1 Reply Last reply
                      0
                      • O Owner drawn

                        Have you added support for WinSock in your project. This happens if you don't have support for winsock in your project. You need to include Winsock2.h And which version of Visual Studio are you using.

                        Jesus Loves:rose:

                        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                        W Offline
                        W Offline
                        waxie
                        wrote on last edited by
                        #11

                        Yes I have included winsock.h in my project - else it would produce symbol not found errors. I'm using VC6.

                        O 1 Reply Last reply
                        0
                        • W waxie

                          Yes I have included winsock.h in my project - else it would produce symbol not found errors. I'm using VC6.

                          O Offline
                          O Offline
                          Owner drawn
                          wrote on last edited by
                          #12

                          Did you link to the Winsock library.

                          Jesus Loves:rose:

                          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                          W 1 Reply Last reply
                          0
                          • O Owner drawn

                            Did you link to the Winsock library.

                            Jesus Loves:rose:

                            --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                            W Offline
                            W Offline
                            waxie
                            wrote on last edited by
                            #13

                            Yes, I have. I don't have errors now but it really doesn't output anything. If it's alright with you, can you check if this code works fine in your pc and outputs your IP ad? Thanks so much. waxie

                            O 1 Reply Last reply
                            0
                            • W waxie

                              Yes, I have. I don't have errors now but it really doesn't output anything. If it's alright with you, can you check if this code works fine in your pc and outputs your IP ad? Thanks so much. waxie

                              O Offline
                              O Offline
                              Owner drawn
                              wrote on last edited by
                              #14

                              Try this one.... You may have to modify it a bit to suit your needs....

                              void CHostNameAndIpDlg::OnBnClickedFind()
                              {
                              WSADATA WSAData;
                              ::ZeroMemory(&WSAData, sizeof(WSAData));
                              //init winsock
                              ::WSAStartup(MAKEWORD(1,0), &WSAData);

                              char szHostName\[MAX\_PATH\];
                              ::gethostname(szHostName, MAX\_PATH);
                              //SetDlgItemText(IDC\_HOSTNAME, szHostName);	
                                   AfxMessageBox(szHostName);
                              char szIps\[128\];
                              
                              struct sockaddr\_in sckAddr;
                              struct hostent \*pHost;
                              
                              pHost = ::gethostbyname(szHostName);
                              
                              for(int index=0; pHost->h\_addr\_list\[index\] != NULL;index++)
                              {
                              	memcpy(&sckAddr.sin\_addr, pHost->h\_addr\_list\[index\], pHost->h\_length);
                              	strcpy(szIps, inet\_ntoa(sckAddr.sin\_addr));
                              	strcat(szIps, ", ");
                              	//SetDlgItemText(IDC\_HOSTIP, szIps);
                                            AfxMessageBox(szIps);
                              	szIps\[0\] = 0;
                              }
                              //close winsock
                              ::WSACleanup();	
                              

                              }

                              AfxMessageBox denotes output. You can replace it with your own output procedure. I have commented my output procedure. First it will display a message box containing host name, second it will display message boxes for each IP found(If there are more that one).

                              Jesus Loves:rose:

                              --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                              W 1 Reply Last reply
                              0
                              • O Owner drawn

                                Try this one.... You may have to modify it a bit to suit your needs....

                                void CHostNameAndIpDlg::OnBnClickedFind()
                                {
                                WSADATA WSAData;
                                ::ZeroMemory(&WSAData, sizeof(WSAData));
                                //init winsock
                                ::WSAStartup(MAKEWORD(1,0), &WSAData);

                                char szHostName\[MAX\_PATH\];
                                ::gethostname(szHostName, MAX\_PATH);
                                //SetDlgItemText(IDC\_HOSTNAME, szHostName);	
                                     AfxMessageBox(szHostName);
                                char szIps\[128\];
                                
                                struct sockaddr\_in sckAddr;
                                struct hostent \*pHost;
                                
                                pHost = ::gethostbyname(szHostName);
                                
                                for(int index=0; pHost->h\_addr\_list\[index\] != NULL;index++)
                                {
                                	memcpy(&sckAddr.sin\_addr, pHost->h\_addr\_list\[index\], pHost->h\_length);
                                	strcpy(szIps, inet\_ntoa(sckAddr.sin\_addr));
                                	strcat(szIps, ", ");
                                	//SetDlgItemText(IDC\_HOSTIP, szIps);
                                              AfxMessageBox(szIps);
                                	szIps\[0\] = 0;
                                }
                                //close winsock
                                ::WSACleanup();	
                                

                                }

                                AfxMessageBox denotes output. You can replace it with your own output procedure. I have commented my output procedure. First it will display a message box containing host name, second it will display message boxes for each IP found(If there are more that one).

                                Jesus Loves:rose:

                                --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                                W Offline
                                W Offline
                                waxie
                                wrote on last edited by
                                #15

                                Thank you very much. I think you deserve a 10. :cool:

                                O 1 Reply Last reply
                                0
                                • W waxie

                                  Thank you very much. I think you deserve a 10. :cool:

                                  O Offline
                                  O Offline
                                  Owner drawn
                                  wrote on last edited by
                                  #16

                                  Remove this from the loop... szIps[0] = 0; or else you won't get all the ip addresses. And put the messagebox in the loop, outside the loop. This will display all the ip addresses together.

                                  Jesus Loves:rose:

                                  --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                                  W 1 Reply Last reply
                                  0
                                  • O Owner drawn

                                    Remove this from the loop... szIps[0] = 0; or else you won't get all the ip addresses. And put the messagebox in the loop, outside the loop. This will display all the ip addresses together.

                                    Jesus Loves:rose:

                                    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                                    W Offline
                                    W Offline
                                    waxie
                                    wrote on last edited by
                                    #17

                                    Okiems. Thank you very much Owner drawn! I really appreciate your help. :) waxie

                                    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