1 /*头文件*/ 2 #include "winsock2.h" 3 4 #pragma comment(lib,"ws2_32.lib") 5 6 /*Hui 获取当前服务器IP*/ 7 WSADATA wsaData; 8 char name[155]; 9 char *ip; 10 PHOSTENT hostinfo; 11 if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 ) 12 { 13 if( gethostname ( name, sizeof(name)) == 0) 14 { 15 if((hostinfo = gethostbyname(name)) != NULL) 16 { 17 ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); 18 printf("当前服务器IP:%s ", ip); 19 } 20 } 21 WSACleanup( ); 22 } 23 /*END*/