Click to See Complete Forum and Search --> : Winsock <-> IP


Ran
May 8th, 1999, 08:30 AM
I'm programming using VC++ 5.0.

The program I'm writing is for both win95 and 98.
The part of the program should show IP number of the computer.

What is the best way to get IP adress, without external dlls'? So far, I have used:

char szHostName[128];
struct hostent * pHost;
CString str = "";
int nCheck = 0;
WSADATA wsaData;
/*
WSAStartup (MAKEWORD( 1, 0 ), &wsaData);
nCheck = gethostname (szHostName, 128);

if( gethostname (szHostName, 128) == 0 ) {
// Get host adresses
int i;

pHost = gethostbyname(szHostName);

for( i = 0; pHost!= NULL && pHost-&gt;h_addr_list[i]!= NULL; i++
int j;

for( j = 0; j &lt; pHost-&gt;h_length; j++ ) {
CString addr;
if( j &gt; 0 ) str += ".";

addr.Format("%u", (unsigned int)((unsigned
char*)pHost-&gt;h_addr_list[i])[j]);
str += addr;
}

Mahmoudo
July 3rd, 1999, 11:41 AM
hi,
here is one method:


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
char tmp[100];
PHOSTENT hostinfo;
CString m_MachineAddr , m_MachineIPAddr;

gethostname(tmp,100);
strcpy(m_MachineAddr.GetBuffer(1),tmp);

hostinfo = gethostbyname(tmp);
m_MachineIPAddr = inet_ntoa (*(struct in_addr *)*hostinfo-&gt;h_addr_list);