#include
#include "winsock2.h "
#include
#include
using namespace std;
int main()
{
struct hostent *thisHost;
struct in_addr in;
char MyName[80];
char *Ip,*ptr;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
return -1;
}
/* Confirm that the WinSock DLL supports 2.0.*/
/* Note that if the DLL supports versions greater */
/* than 2.0 in addition to 2.0, it will still return */
/* 2.0 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
WSACleanup();
return -1;
}
if(gethostname(MyName,80)==SOCKET_ERROR)
return -1;
if(!(thisHost=gethostbyname(MyName)))
return -1;
memset((void *)&in,sizeof(in),0);
in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]);
if(!(ptr=inet_ntoa(in)))
return -1;
WSACleanup();
Ip=new char[strlen(ptr)+1];
strcpy(Ip,ptr);
cout << Ip;
delete[] Ip;
return 0;
}
阅读(1958) | 评论(0) | 转发(0) |