Get hostname and ip address of local computer

This code will get the host name and ip for the computer on which the code is run. This works in Visual C++ 5 SP1/2 on NT4 and 95.

Requirements

#include <winsock2.h>

Link with Wsock32.lib

That's It.
{
      WORD wVersionRequested;
      WSADATA wsaData;
      char name[255];
      CString ip;
      PHOSTENT hostinfo;
      wVersionRequested = MAKEWORD( 2, 0 );

      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
      {

            if( gethostname ( name, sizeof(name)) == 0)
            {
                  if((hostinfo = gethostbyname(name)) != NULL)
                  {
                        ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                  }
            }
            
            WSACleanup( );
      } 
}



Comments

  • two bugs in this code!!! below is correct code:)

    Posted by drucikx on 04/02/2005 05:22am

    this code has a few bugs!;] correct code is below: #include // there were first bug;] { WORD wVersionRequested; WSADATA wsaData; char name[255]; char *ip; // there were two bug ip = (char *)malloc(16); PHOSTENT hostinfo; wVersionRequested = MAKEWORD( 2, 0 ); if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) { if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); } } WSACleanup( ); } }

    Reply
  • how to get host name

    Posted by Legacy on 02/20/2004 12:00am

    Originally posted by: Johan Eliasson

    GetComputerName()...
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getcomputername.asp

    Reply
  • How to broadcast to a network

    Posted by Legacy on 01/28/2003 12:00am

    Originally posted by: Gabriel

    Can anyone show me a program how to broadcast to a network from any host and get a reply wheather its alive or not
    

    Reply
  • Good code

    Posted by Legacy on 08/13/2001 12:00am

    Originally posted by: Caleb

    Hey thanks alot for the code :)

    Later,
    Caleb

    Reply
  • Problems with getaddrinfo() function?

    Posted by Legacy on 07/16/2001 12:00am

    Originally posted by: Thomas Sch�fter

    Wanted to use getaddrinfo function instead gethostbyname (recommended in the MSDN Library)
    Cannot compile and link because function not available. ws2tcpip.h is from April 1998. I am using Visual Studio 6 with service pack 5.
    Why is the new function not available?

    Reply
  • ~How to obtain FQDN of local host?

    Posted by Legacy on 06/25/2001 12:00am

    Originally posted by: Art S

    Both gethobyname & gethobyaddr retrurns a short(i.e netbios)
    
    name of local computer, not FQDN.But how to obtain FQDN?
    Ex:
    gethostname(szHostName, 128);
    // will return SERVER1
    pHost = gethostbyname(szHostName);
    printf("%s",pHost->h_name);
    // will also return SERVER1 ! , not dlp1-24.loc.isp.com

    Other programm like MIRC ,say that
    /dns 195.66.45.56(my ip) resolved to SERVER1
    but
    /dns 195.66.45.57 resolved to FQDN dlp1-25.loc.isp.com

    Can i get FQDN for myself host?And how i'm should do it?

    Reply
  • A small change will solve problems

    Posted by Legacy on 04/22/2001 12:00am

    Originally posted by: Nitin

    The program is mixed the versions of winsock.

    Version 1 can be
    ================
    #include "winsock2.h"
    wVersionRequested = MAKEWORD( 2, 0 );
    and link WS2_32.lib

    Version 2 can be
    ================
    #include "winsock.h"
    Use wVersionRequested = MAKEWORD( 1, 1 );
    and link with WSOCK32.lib

    Thats it.

    Reply
  • Big waste of time writing that....

    Posted by Legacy on 11/24/2000 12:00am

    Originally posted by: Cero

    ...if you ONLY want the lousy IP and Hostname of the local computer why bother with all that crap!
    
    

    WORD wVersionRequested; <- what's with programming nerds that they have to make the variables long??
    wVersionRequested???? Example: CInternetSession *session that's it!! not CInternetSession *myInternetSession <-waste of time..shorten that shit.

    and as for this program why the hell would you want to waste your time writing all that when you can easily just add the winsock control give it a variable (something short)not m_ThisIsMyWinSockControl

    and just do this on either a button function , messagebox ..whatever!!

    MessageBox(m_winsock.GetLocalIP(),NULL,0);
    or
    MessageBox(m_winsock.GetLocalHostName(),NULL,0);

    Reply
  • Useful and timesaving

    Posted by Legacy on 11/17/2000 12:00am

    Originally posted by: Rainer Sgorzali

    Thanks for Your help!

    I've been looking for ages in Windows-API documentation and on "msdn"-pages. Without result. After 5 minutes of research here at codeguru I've found Your source code. 10 minutes later I hade inserted the code into my project aand it worked well from the beginning.

    THANX, Rainer

    Reply
  • Thanks Jeff !

    Posted by Legacy on 10/30/2000 12:00am

    Originally posted by: HuiFang

    This is really useful, and solved my problem...^^...

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds