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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds