Get IP of local (host) computer

Often when programming simple TCP/IP server (for monitoring of services and other non-GUI-based applications, etc.) we need to know IP of the host computer so that clients can connect to it; to retrieve this IP from within the server-side program, use the following snippet:

WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 0);

if (WSAStartup(wVersionRequested, &wsaData ) != 0)
{
    printf("WSAStartup() failed!n");
    return 0;
}

// Get the local host information
hostent* localHost = gethostbyname("");
char* localIP = inet_ntoa(*(struct in_addr *)*localHost->h_addr_list);

printf("Local host IP is: %sn", localIP);

WSACleanup();

Includes
WinSock2.h, Ws2tcpip.h

Link libraries
ws2_32.lib

Once I get some time on my hands, I’ll clean up and post the code of my WinSock-based TCP/IP library for simple development of both server and client side.

6 responses to “Get IP of local (host) computer

  1. It’s actuaally a nice and useful piece of info.
    I am happy that you just shared this seful info with us.
    Please keep us informed like this. Thanks for sharing.

  2. Find and buy this item through at the Geo – Coin – Store.
    It hovers, glides and even streaks across rooms, never touching the ground.
    You want to buy your mom something special for her birthday but all you can find are things that are way out of your budget plan.

  3. Great beat ! I would like to apprentice at the same time as you amend your site,
    how can i subscribe for a weblog site? The account aided me a appropriate deal.
    I had been tiny bit acquainted of this your broadcast provided shiny
    transparent idea

  4. You can remain calm and have peace no matter what’s going
    on around you. Once your tomatoes start to grow,
    reduce your watering a bit-this will encourage the plants to concentrate sugars, giving your
    tomatoes a more intense taste. Plot: The plot of this season is much like
    the plot of Season 1 where there is one serial killer for the main story.

  5. You can remain calm and have peace no matter what’s going on around you.
    This is one event that you’ll want to bring the entire family to.
    The fiery matches between these underdogs and frontrunners will
    likely involve a few upsets and projected wins.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s