9 Replies - 2662 Views - Last Post: 21 November 2012 - 02:48 PM Rate Topic: -----

#1 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Please help me get the IP address

Posted 17 November 2012 - 09:04 PM

Hi everyone at dic! I'm doing a final project for my C++ class where I wish to (it's a custom topic project) obtain the ip address of the computer the program I do runs on. So I'm using Visual C++ to do this, and in a windows forms application. I've got some code from this page ( http://stackoverflow...s-from-sockaddr ) where they suggest to convert the integer value, client.sin_addr.s_addr, to the standard dot notation for an ip address using inet_ntoa.

I'm basically using a stringstream to obtain the value converted and then convert it to a string, then to a System::String (String^), to set the String^ as text of a label, like this:

stringstream s;
s << "...";
string ss = s.str();
String^ sss = gcnew String(ss.c_str());
titlelabel->Text = L"" + sss;



The problem is the value "..." . I've tried doing:

 s << inet_ntoa(client.sin_addr.s_addr);


but then I get

error C2664: 'inet_ntoa': cannot convert parameter 1 from 'ULONG' to 'in_addr'


If I take away the .s_addr, I get it to run, but the IP address that comes out is 100.235.64.0 or 116.231.44.0 or 244.234.69.0 or other values EXCEPT my IPv4, which starts with 192.

If I convert like this:

char buf[INET_ADDRSTRLEN] = "";
s << inet_ntop(AF_INET, &client.sin_addr.s_addr, buf, sizeof buf);



I still get anything except my ipv4. And if I take away the .s_addr, I still don't get the ipv4

So how should I convert? Or this is method of getting the IP address even correct? What do you suggest I should do, that's easy to understand given my low level in network programming (ESPECIALLY in c++)

Please let me know. And as always, thanks for any help or suggestion

Is This A Good Question/Topic? 0
  • +

Replies To: Please help me get the IP address

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Please help me get the IP address

Posted 18 November 2012 - 12:19 AM

inet_ntoa wants an instance of 'in_addr'. My immediate impression is that 'client.sin_addr' is just that, so try this.

s << inet_ntoa(client.sin_addr);


edit: sorry, misread the question.

This post has been edited by Aphex19: 19 November 2012 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Please help me get the IP address

Posted 18 November 2012 - 04:01 AM

First link in a Google search of inet_ntoa leads to Beej's Guide, which states:

Quote

// ALL THESE ARE DEPRECATED! Use inet_pton() or inet_ntop() instead!!


So the first thing you should do is follow that advice, noting there is a link at the bottom of that page to inet_ntop, which furthermore has a complete example of how to use it.

The Internet: amazing.
Was This Post Helpful? 1
  • +
  • -

#4 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: Please help me get the IP address

Posted 19 November 2012 - 03:08 PM

@Aphex19: I tried that, but like I said I get anything except the ipv4. While the number I get is in dot notation and while it resembles the ip address, it's not similar to the one I get in command prompt using 'ipconfig'

@JackOfAllTrades: I did try using inet_ntop, like below:

char buf[INET_ADDRSTRLEN] = "";  
s << inet_ntop(AF_INET, &client.sin_addr.s_addr, buf, sizeof buf); 



but I get the same pseudo-ip instead of the ipv4
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Please help me get the IP address

Posted 19 November 2012 - 03:30 PM

How are you populating the value in client?
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 777
  • View blog
  • Posts: 2,297
  • Joined: 20-March 10

Re: Please help me get the IP address

Posted 19 November 2012 - 11:36 PM

Hi,

There are lots of ways of doing this here is one
where you store the values in a struct...


#include <iostream>
#include <winsock2.h>


 struct IPv4{

  int b1,b2,b3,b4;
 };
bool getMyIP(struct IPv4* myIP)
{
    char szBuffer[1024];


    WSADATA wsaData;
    WORD wVersionRequested = MAKEWORD(2, 0);
    if(WSAStartup(wVersionRequested, &wsaData) != 0)
        return false;



    if(gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR)
    {

      WSACleanup();

      return false;
    }

   hostent *host = gethostbyname(szBuffer);
    if(host == NULL)
    {

      WSACleanup();

      return false;
    }
    in_addr addr;
    //Obtain the computer's IP
    for (int i = 0; host->h_addr_list[i] != 0; ++i)
    {

        memcpy(&addr, host->h_addr_list[i], sizeof(in_addr));
        std::cout << "IPv4 Address:  " << inet_ntoa(addr) << std::endl;



    }

    myIP->b1 = (int)addr.S_un.S_un_b.s_b1;
    myIP->b2 = (int)addr.S_un.S_un_b.s_b2;
    myIP->b3 = (int)addr.S_un.S_un_b.s_b3;
    myIP->b4 = (int)addr.S_un.S_un_b.s_b4;
    WSACleanup();

    return true;
}
    int main()
    {
       IPv4 ip;

        if(!getMyIP(&ip))
        {
          std::cout <<"Theres been an error !" << std::endl;
          return 1;
        }
        else
        std::cout << "IPv4 address stored in struct: ";
        std::cout << ip.b1<<"."<<ip.b2<<"."<<ip.b3<<"."<<ip.b4<< std::endl;
        std::cout << "Check that both addresses are the same, IPv4 address now stored in struct IPv4" ;
        return 0;
    }





Was This Post Helpful? 2
  • +
  • -

#7 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: Please help me get the IP address

Posted 20 November 2012 - 09:35 PM

@JackofTrades: The only thing that happens to client before calling the accept() function is

sockaddr_in client;
client.sin_family = AF_INET;



I'm new at socket programming in C++ and while I don't need to get the ip address, I'm doing it as extra flair for my final project

@snoopy11: Thank you! Your code worked when I ran it, and while I do get a number that starts with 192, when I look it up on command prompt with ipconfig, the ipv4 produced/fetched corresponds to that of 'Ethernet adapter VirtualBox Host-Only Network'

Is this because I have Oracle VirtualBox installed? (though I hardly ever run my virtual pc, and I don't run visual studio in it). Is a virtual pc's ip address supposed to take preference?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Please help me get the IP address

Posted 21 November 2012 - 04:04 AM

Yes, VirtualBox (or VMware, or whatever virtualization program you use) sets up its on network interface on your system, with an internal IP address; that may be the 192 address.
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 777
  • View blog
  • Posts: 2,297
  • Joined: 20-March 10

Re: Please help me get the IP address

Posted 21 November 2012 - 04:15 AM

Hi,

The code I gave you should enumerate all the IPv4 addresses on your system and put the last one found in the IPv4 structure if your getting VirtualBoxes ipv4 address as the last one I would modify the code so you return the one you need or modify the IPv4 structure so it returns all the ip addresses.

You can have one or more IPv4 addresses associated with the one computer.

On my system it only returns one IPv4 and that one matches my ipconfig result associated with my WLAN.

If you are on a wide area network it will also return the networks IPv4 address for instance.

It all depends on your setup.

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#10 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: Please help me get the IP address

Posted 21 November 2012 - 02:48 PM

Thank you =) All I did was comment out the loop and write the line within it replacing the i for 0 as index:

memcpy(&addr, host->h_addr_list[0], sizeof(in_addr));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1