Article ID: 183988
Article Last Modified on 7/1/2004
#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;
}
Keywords: kbhowto kbwinsock kbnetwork kbip KB183988