📄 getip.cpp
字号:
/*
by wenyy
EMail:wyy_cq@188.net
homePage: vchelp@163.net
*/
#include "windows.h"
#include <winsock.h>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void get_ip(void)
{// get multi-IP address
char szHostName[128];
char szT[20];
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++ )
{
char str[100];
char addr[20];
int j;
str[0]='\0';
LPCSTR psz=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
printf("NO %d IP Address : %s\n",i+1,psz);
/*
for( j = 0; j < pHost->h_length; j++ )
{
//CString addr;
addr[0]='0';
if( j > 0 )
strcat(str,".");
sprintf(addr,"%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
strcat(str,addr);
}*/
// printf("no %d : %s\n",i,str);
// str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
}
}
}
void print_ip(void)
{//only can get one IP address
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
//CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
LPCSTR ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
printf("%s\n",ip);
}
}
WSACleanup( );
}
}
void main(void)
{
print_ip();
get_ip();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -