Get hostname and ip address of local computer
Posted
by Jeff Lundgren
on August 7th, 1998
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:22amthis 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( );
}
}
Replyhow to get host name
Posted by Legacy on 02/20/2004 12:00amOriginally posted by: Johan Eliasson
GetComputerName()...
Replyhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getcomputername.asp
How to broadcast to a network
Posted by Legacy on 01/28/2003 12:00amOriginally posted by: Gabriel
ReplyGood code
Posted by Legacy on 08/13/2001 12:00amOriginally posted by: Caleb
Hey thanks alot for the code :)
Later,
Caleb
Reply
Problems with getaddrinfo() function?
Posted by Legacy on 07/16/2001 12:00amOriginally posted by: Thomas Sch�fter
Wanted to use getaddrinfo function instead gethostbyname (recommended in the MSDN Library)
ReplyCannot 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?
~How to obtain FQDN of local host?
Posted by Legacy on 06/25/2001 12:00amOriginally posted by: Art S
ReplyA small change will solve problems
Posted by Legacy on 04/22/2001 12:00amOriginally 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.
ReplyBig waste of time writing that....
Posted by Legacy on 11/24/2000 12:00amOriginally posted by: Cero
ReplyUseful and timesaving
Posted by Legacy on 11/17/2000 12:00amOriginally 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
ReplyThanks Jeff !
Posted by Legacy on 10/30/2000 12:00amOriginally posted by: HuiFang
This is really useful, and solved my problem...^^...
ReplyLoading, Please Wait ...