Can somebody help me why this code fails in linux

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
Raghu
Experienced Solver
Experienced Solver
Posts: 79
Joined: Wed Oct 19, 2005 11:33 am

Can somebody help me why this code fails in linux

Post by Raghu » Thu Mar 02, 2006 11:33 am

Code: Select all

wxString BasicFrame::GetMyIp() 
{ 
    wxIPV4address MyIp;
    wxString MyHostName = MyIp.Hostname(); /* Get my hostname */
    wxString sMyIp;	
    bool bRet;
    bRet = MyIp.Hostname(MyHostName); /* Set MyIp to my Hostname */
    if (bRet == true) 
   {
      sMyIp = MyIp.IPAddress(); /* Get Ip address of my Hostname */
   }
   else
   {
       sMyIp = "0.0.0.0";
   }
   return sMyIp;
}
This code returns the Ipaddr of the system in windows. What when I run this in linux it trows error saying "Trying to solve NULL hostname:giving up" and returns 0.0.0.0. Hostname(MyHostName) is returning false.

regards

protocol
Moderator
Moderator
Posts: 680
Joined: Wed Jan 18, 2006 6:13 pm
Location: Dallas, TX
Contact:

Post by protocol » Fri Mar 10, 2006 6:06 am

Try creating another instance of wxIPV4address and use the new instance to set MyIp to your Hostname.

Let me know if it works.
/* UIKit && wxWidgets 2.8 && Cocoa && .Net */
QuRegExmm
wxPCRE & ObjPCRE - Regex It!

fgleich
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 07, 2005 4:57 pm
Location: NM USA
Contact:

Post by fgleich » Tue Mar 28, 2006 10:02 pm

some linux systems don't set the hostname. In a shell, type in "hostname", and see it anything is set. If not, that's the problem. There should be some administration documentation for your flavor on setting the hostname.

good luck

jlh
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Mar 28, 2006 3:03 pm
Location: Switzerland

Re: Can somebody help me why this code fails in linux

Post by jlh » Tue Mar 28, 2006 11:01 pm

Take this fragment:

Code: Select all

wxIPV4address MyIp;
wxString MyHostName = MyIp.Hostname(); /* Get my hostname */
What makes you believe that this will set MyHostName to your hostname? Neither in wxGTK nor in the documentation did I find anything that says that the default constructor of wxIPV4address would set the object to the machine's IP, so that you could retrieve its hostname with wxIPV4address::Hostname().

I didn't find this explicitely in the documentation either, but a glance at the wxGTK source hints at that the default constructor of wxIPV4address will set the IP to all zeros and the hostname to the empty string (which is why you get that error message).

This makes me wonder why this works on windows. At best, this would be an undocumented and unportable feature.

EDIT: This works for me (linux/wxGTK):

Code: Select all

#include <wx/socket.h>
#include <wx/utils.h>
wxString hostname = wxGetHostName(); /* or wxGetFullHostName() */
wxIPV4address ipaddr;
ipaddr.Hostname(hostname);
wxString ipstring = ipaddr.IPAddress();

Post Reply