The Local IP Address

How to use WinSock to get Information about the Local Host.

© Guy Lecky-Thompson

Apr 24, 2007

We look at solving a typical networking problem - what is the local IP address of a computer, apart from the usual localhost value 127.0.0.1?


We all know that if we type in ipconfig on a DOS box, we can take a look at the bound IP addresses, right? But how do we do something similar in an application?

If you've tried using gethostbyname, you'll be disappointed. Because that returns 127.0.0.1; largely due to the fact that you've probably supplied "localhost' as the host name. This is valid, and the value returned is logical, just useless.

We have the solution, though, and it is really very simple. Before I point you in the right direction, just take a moment to peruse it's usefulness.

The reason I was looking for this is because I'm working on a little Windows application that will anchor itself to the top-right corner of the screen, has no client area, and resembles a floating title bar. In the title bar is ... the local IP address of the machine, periodically updated, and very useful when you're troubleshooting the local network, or VPN.

If you want a copy of this wonderful little tool, then all you have to do is email me - computerprogramming@suite101.com - and I'll get one out to you right away. Meanwhile, learn with me as we look at the wonderful world of socket programming, and localhost.

The actual article link : Socket Programming gethostbyname.

Happy (network) coding!

Guy.


Post this Blog to facebook Add this Blog to del.icio.us! Digg this Blog furl this Blog Add this Blog to Reddit Add this Blog to Technorati Add this Blog to Newsvine Add this Blog to Windows Live Add this Blog to Yahoo Add this Blog to StumbleUpon Add this Blog to BlinkLists Add this Blog to Spurl Add this Blog to Google Add this Blog to Ask Add this Blog to Squidoo