How to get the IP

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
monteaup
In need of some credit
In need of some credit
Posts: 7
Joined: Tue Aug 02, 2005 9:21 am

How to get the IP

Post by monteaup »

I have 2 NICs installed. How can I get the IP of the both or a specific NIC ?

wxIPV4address::IPAddress tells me the IP only of the first NIC.
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel »

if its win32 use ipconfig.exe and get the output? idk
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Have you looked into the wxIP4Address source and checked which of the two NIC's is chosen as the one that is returned? Maybe you can either tweak it or add the missing NIC.

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
wxDiilbert
Earned a small fee
Earned a small fee
Posts: 16
Joined: Tue Feb 08, 2005 3:52 am

Post by wxDiilbert »

If anyone gets this figured, post the results. I was been looking for a solution to this for awhile.
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: How to get the IP

Post by Ryan Norton »

monteaup wrote:I have 2 NICs installed. How can I get the IP of the both or a specific NIC ?

wxIPV4address::IPAddress tells me the IP only of the first NIC.
Good question. Isn't the one IPAddress returns determined by the operating system - i.e. the user - though?
[Mostly retired moderator, still check in to clean up some stuff]
KevinHock
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Sat Sep 04, 2004 1:49 pm
Location: Ohio, USA
Contact:

Post by KevinHock »

The following code will populate a wxArrayString and an array of wxUint32s with both the string and long forms of the computer's assigned IP addresses (up to 12). If you need more than 12, just adjust the array and the loop. This code should work on all major platforms.

One other note: I have a special class for conversions, so you won't have my CConversions:: method used here, but you can substitute your favorite way of converting an int to a string.

Code: Select all

	char szHostName[128];
	wxString str;
	struct hostent * pHost;
	int i = 0, j = 0;

	wxArrayString StrAddresses;
	wxUint32 longAddresses[12];

	if(gethostname(szHostName, 128) == 0 )
	{
		pHost = gethostbyname(szHostName);
		for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL && i < 12; i++ )
		{
			for( j = 0; j < pHost->h_length; j++ )
			{
				if( j > 0 )
				{
					str += _T(".");
				}
				str += CConversions::UnsignedLongTowxString((unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
			}

			// Do not store an address if it is localhost
			if (str != _T("127.0.0.1"))
			{
				// Now that we have an ip address, store it
				StrAddresses.Add(str);
			}
			str.Clear();
		}
	}
I have seen this question many times so I hope that this code will help many people.
KevinHock
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Sat Sep 04, 2004 1:49 pm
Location: Ohio, USA
Contact:

Post by KevinHock »

BTW, in case you don't have a favorite way of converting numbers to strings, you can always use wxString::Format("%d", i) where i is the number you want to convert to a string. A wxString is returned by Format.
Post Reply