📄 project1.cpp
字号:
#pragma hdrstop
#include <condefs.h>
#include <stdio.h>
#include <windows.h>
LPSTR GetHostIpAddress();
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
char *IP;
IP=GetHostIpAddress();
printf("ip=%s\n",IP);
getchar();
return 0;
}
//===========================================
LPSTR GetHostIpAddress()
{
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 NULL;
}
/* 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 NULL;
}
if(gethostname(MyName,80)==SOCKET_ERROR)
return NULL;
if(!(thisHost=gethostbyname(MyName)))
return NULL;
memset((void *)&in,sizeof(in),0);
in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]);
if(!(ptr=inet_ntoa(in)))
return NULL;
WSACleanup( );
Ip=new char[strlen(ptr)+1];
strcpy(Ip,ptr);
return Ip;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -