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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • 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 …

Most Popular Programming Stories

More for Developers

RSS Feeds