Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: IpAddress::GetLocalAddress() returns 127.0.0.1  (Read 9692 times)

0 Members and 1 Guest are viewing this topic.

JayArby

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
IpAddress::GetLocalAddress() returns 127.0.0.1
« Reply #15 on: January 26, 2012, 09:11:36 pm »
Oh well, thanks for the help, anyway. Please let me know if you find any other way I could get this to work. I will be researching it myself.

JayArby

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
IpAddress::GetLocalAddress() returns 127.0.0.1
« Reply #16 on: January 26, 2012, 09:25:16 pm »
Okay, I googled around and found something that works.

Code: [Select]
#include <iostream>
#include <winsock.h>

int main(int argc, char** argv)
{
    WSAData wsaData;
    if( WSAStartup(MAKEWORD(1, 1), &wsaData) != 0 )
        return 1;
   
    char ac[80];
    if(gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
    {
        cerr << "Error " << WSAGetLastError() <<
                " when getting local host name." << endl;
        return 1;
    }
   
    cout << "Host name is " << ac << "." << endl;
   
    struct hostent *phe = gethostbyname(ac);
    if(phe == 0)
    {
        cerr << "Bad host lookup." << endl;
        return 1;
    }
   
    for(int i = 0; phe->h_addr_list[i] != 0; ++i)
    {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
    }
   
    WSACleanup();
   
    getchar();
    return 0;
}


(http://stackoverflow.com/questions/122208/get-the-ip-address-of-local-computer)

I don't know what WSA is, but this gives me the right address. It gives multiple addresses, though. Would it be possible to somehow incorporate this technique into SFML for those of us with weird network switches? :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
IpAddress::GetLocalAddress() returns 127.0.0.1
« Reply #17 on: January 27, 2012, 08:14:33 am »
This technique doesn't seem to be 100% reliable (because it's based on the host name). And I found this in the comments:
Quote
The method listed on the page above doesn't appear to work for DHCP assigned addresses. Ultimately the gethostbyname command scans the /etc/hosts file for the hostname provided. I've found that in our lab we have an entry such as 127.0.0.2, while ifconfig shows an address of 10.194.157.197


Quote
I don't know what WSA is

The Windows specific initialization required for sockets. Don't worry about this.

Quote
It gives multiple addresses, though

And SFML is not yet ready for that. There's an open issue in the task tracker (with less "deprecated" functions to get the same results, by the way).
Laurent Gomila - SFML developer

JayArby

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
IpAddress::GetLocalAddress() returns 127.0.0.1
« Reply #18 on: January 27, 2012, 04:11:44 pm »
Would there be some way to fall back on that technique if the first one fails? And just return the first address it finds or something? Seems like that would be better than nothing, at least.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
IpAddress::GetLocalAddress() returns 127.0.0.1
« Reply #19 on: January 27, 2012, 04:50:48 pm »
This problem will be solved, of course, but not before SFML 2.0 is released, sorry.
Laurent Gomila - SFML developer

JayArby

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
IpAddress::GetLocalAddress() returns 127.0.0.1
« Reply #20 on: January 27, 2012, 05:26:01 pm »
Ok, thanks anyway.

 

anything