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 do I output the IP Address after getting computer name??

How do I output the IP Address after getting computer name??

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
6 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.
  • I Offline
    I Offline
    IrishSonic
    wrote on last edited by
    #1

    Hiya I am writing a window sockets program. I can find loads of examples of setting the IP address and then displaying that, but I can not find out how to display the IP Address once I have got the computer name..I have this so far: char szHostName[200]; struct hostent *host; gethostname( szHostName,strlen( szHostName )); host = gethostbyname( szHostName ); // now display the IP Address of the host?? I have seen the inet_addr function, but don't know how to use it..

    B C D 3 Replies Last reply
    0
    • I IrishSonic

      Hiya I am writing a window sockets program. I can find loads of examples of setting the IP address and then displaying that, but I can not find out how to display the IP Address once I have got the computer name..I have this so far: char szHostName[200]; struct hostent *host; gethostname( szHostName,strlen( szHostName )); host = gethostbyname( szHostName ); // now display the IP Address of the host?? I have seen the inet_addr function, but don't know how to use it..

      B Offline
      B Offline
      Bartosz Bien
      wrote on last edited by
      #2

      Example: in_addr *addr = (in_addr*)*host->h_addr_list; CString s; s.Format("%d.%d.%d.%d", addr->s_b1, addr->s_b2, addr->s_b3, addr->s_b4); Regards, BB

      I 1 Reply Last reply
      0
      • B Bartosz Bien

        Example: in_addr *addr = (in_addr*)*host->h_addr_list; CString s; s.Format("%d.%d.%d.%d", addr->s_b1, addr->s_b2, addr->s_b3, addr->s_b4); Regards, BB

        I Offline
        I Offline
        IrishSonic
        wrote on last edited by
        #3

        Hiya, thanks for that and tried it but it says that s_b1 is not a member of in_addr. Which it isn't. I put it in exactly as suggusted. But S_un is the member of in_addr .i.e addr->S_un What can I do to fix this?? Thanks again, grahamoj

        B 1 Reply Last reply
        0
        • I IrishSonic

          Hiya, thanks for that and tried it but it says that s_b1 is not a member of in_addr. Which it isn't. I put it in exactly as suggusted. But S_un is the member of in_addr .i.e addr->S_un What can I do to fix this?? Thanks again, grahamoj

          B Offline
          B Offline
          Bartosz Bien
          wrote on last edited by
          #4

          OK, I mispelled this. I guess you may use: s.Format("%d.%d.%d.%d", addr->S_un.S_un_b.s_b1, addr->S_un.S_un_b.s_b2, addr->S_un.S_un_b.s_b3, addr->S_un.S_un_b.s_b4); Regards, BB

          1 Reply Last reply
          0
          • I IrishSonic

            Hiya I am writing a window sockets program. I can find loads of examples of setting the IP address and then displaying that, but I can not find out how to display the IP Address once I have got the computer name..I have this so far: char szHostName[200]; struct hostent *host; gethostname( szHostName,strlen( szHostName )); host = gethostbyname( szHostName ); // now display the IP Address of the host?? I have seen the inet_addr function, but don't know how to use it..

            C Offline
            C Offline
            csc
            wrote on last edited by
            #5

            Hi ... you may try this code snippet .... the result should be something like 123.456.789.123 ... hope it will help you ;) struct hostent * pHost = gethostbyname(szHostName); if(pHost != NULL) { for(int i = 0; (pHost->h_addr_list[i]!= NULL); i++ ) { CString csIP = ""; for(int j = 0; j < pHost->h_length; j++ ) { if( j > 0 ) csIP += "."; CString addr = ""; addr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]); csIP += addr; } AfxMessageBox( csIP ); } }

            1 Reply Last reply
            0
            • I IrishSonic

              Hiya I am writing a window sockets program. I can find loads of examples of setting the IP address and then displaying that, but I can not find out how to display the IP Address once I have got the computer name..I have this so far: char szHostName[200]; struct hostent *host; gethostname( szHostName,strlen( szHostName )); host = gethostbyname( szHostName ); // now display the IP Address of the host?? I have seen the inet_addr function, but don't know how to use it..

              D Offline
              D Offline
              Dudi Avramov
              wrote on last edited by
              #6

              use inet_ntoa fucntion: char szIP[16]; struct hostent *host = gethostbyname( szHostName ); struct in_addr *InAddr = (struct in_addr*)host->h_addr_list[0]; strcpy(szIP,inet_ntoa(*InAddr));

              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