对于获得动态分配的IP地址,你可这样:(供你参考)
char szhostname[128];if( gethostname(szhostname, 128) == 0 )
{
// get host adresses
struct hostent * phost;
int i;
phost = gethostbyname(szhostname);
for( i = 0; phost!= null && phost->h_addr_list[i]!= null; i++ )
{
cstring str;
int j;
for( j = 0; j h_length; j++ )
{
cstring addr;
if( j > 0 )
str += ".";
addr.format("%u", (unsigned int)((unsigned
char*)phost->h_addr_list[i])[j]);
str += addr;
}
// str now contains one local ip address - do whatever you want to do with it (probably add it to a list)
}
}
char szhostname[128];if( gethostname(szhostname, 128) == 0 )
{
// get host adresses
struct hostent * phost;
int i;
phost = gethostbyname(szhostname);
for( i = 0; phost!= null && phost->h_addr_list[i]!= null; i++ )
{
cstring str;
int j;
for( j = 0; j h_length; j++ )
{
cstring addr;
if( j > 0 )
str += ".";
addr.format("%u", (unsigned int)((unsigned
char*)phost->h_addr_list[i])[j]);
str += addr;
}
// str now contains one local ip address - do whatever you want to do with it (probably add it to a list)
}
}
WORD wVersionRequested;
WSADATA wsaData;
char szname[255]; //主机名
TCHAR ip[MAX_PATH];//ip
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
lstrcpy(ip ,inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list));
}
}
WSACleanup( );
}
WORD wVersionRequested;
WSADATA wsaData;
char name[255]; //主机名
TCHAR ip[MAX_PATH];//ip
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
lstrcpy(ip ,inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list));
}
}
WSACleanup( );
}
The following sample code demonstrates how to programmatically retrieve the IP address of the remote peer by using Windows Sockets gethostbyname() API with "ppp_peer". #include <windows.h>
#include <winsock.h> #define MAXLEN 64 BOOL GetIpAddress(char * hostname); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdSHow)
{
WSADATA wsadata;
HOSTENT *lpHost=NULL;
char hostname[MAXLEN];
WCHAR msg[128];
int ret, addrlen;
SOCKET sock;
struct sockaddr_in dest, retaddr;
WSAStartup(0x0101, &wsadata); if ((ret = gethostname(hostname, MAXLEN)) != 0)
{
wsprintf(msg, L"Error calling gethostname: %d", ret);
MessageBox(NULL, msg, NULL, MB_OK);
}
else
{
wsprintf(msg, L"Hostname is: %S", hostname);
MessageBox(NULL, msg, L"Hostname", MB_OK);
} // Print the local IP address.
MessageBox(NULL, L"Local IP address is", L"Address", MB_OK);
GetIpAddress (hostname);
// Print the remote peer IP address.
MessageBox(NULL, L"Remote IP address is", L"Address", MB_OK);
GetIpAddress ("ppp_peer"); WSACleanup(); return 0;
} BOOL GetIpAddress(char *hostname)
{
WCHAR msg[128];
HOSTENT *lpHost=NULL;
struct sockaddr_in dest; lpHost = gethostbyname(hostname);
if (lpHost == NULL)
{
wsprintf(msg, L"gethostbyname failed: %d", WSAGetLastError());
MessageBox(NULL, msg, NULL, MB_OK);
}
else
{
for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
{
memcpy(&(dest.sin_addr), lpHost->h_addr_list[i],
lpHost->h_length);
wsprintf(msg, L"IP address is: '%S'",
inet_ntoa(dest.sin_addr));
MessageBox(NULL, msg, L"IP Address", MB_OK);
} }
return 0;
}