Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the IP address of my computer 1

Status
Not open for further replies.

SCervantes

Programmer
Nov 9, 2004
2
MX
Hello,

Any clues on how to get the IP address of the computer that runs the code?

I am coding an application that should show the IP in its main window for user's information.

Thanks,
Sergio
 
I've not done it but I'm told that you need to use the Windows APIs gethostname, gethostbyname, or GetAdapterInfo(). I did find one section of code that might help. It's by Michel Leunen (
Code:
char host_name[64]; //holds the name
gethostname(host_name,sizeof(host_name));
hostent *he = gethostbyname(host_name);
in_addr aAddress;
memcpy(&address,he->h_addr_list[0],he->h_length);
char IP_address[64]; //holds the IP address
strcpy(IP_address,inet_ntoa(address));

James P. Cottingham
-----------------------------------------
To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.
 
if you use the fastnet components (any of the server ones etc)

its as simple as this

String Bob = NMGeneralServer->LocalIP;
 
Thanks, guys, for your help! Now my code is working all right! I tried both options and worked well.

Maybe the final code (by using Win API) is helpful to somebody else, so, it is below.

Thanks again,
Sergio


//----------------------------------------
// *** get locahost's name
char host_name[64];
struct hostent *datos_host;

// *** initialize the winsocket
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err == 0 )
{
//*** check the winsock compatibility

if ( LOBYTE( wsaData.wVersion ) == 2 &&
HIBYTE( wsaData.wVersion ) == 2 )
{
/* The WinSock DLL is acceptable. Proceed. */

if( !gethostname(host_name,sizeof(host_name)))
{
//*** finally, I get the IP
struct hostent *he;
in_addr address;

if( he = gethostbyname(host_name))
{
memcpy( &address, he->h_addr_list[0]
, he->h_length);
char IP_address[64];
strcpy(IP_address,inet_ntoa(address));

//*** show hostname and its IP
MessageBox( AnsiString( host_name)
+ " - " + IP_address;
}
else
{
int i_error = WSAGetLastError();
ShowMessage( AnsiString( "¡Puff! - Error #")
+ i_error
+ " when running gethostbyname.");
}
}
else
{
ShowMessage( AnsiString( "Error #")
+ (int)WSAGetLastError()
+ " when running 'gethostname'.");
}

}
else
{
ShowMessage( "Unable to start the 'winsock'.");
}
}
else
{
WSACleanup( );
ShowMessage( "Can't find any compatible 'winsock'");
}
//----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top