#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define WIN32 a.c
#ifdef WIN32
#include "windows.h"
#include <winsock.h>
#pragma comment(lib,"ws2_32")
#else
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#endif
#define BUFLEN 4086
#define HOSTDOMAIN "www.ip138.com"
#define URLPATH "/ips138.asp?action=2&ip="
#define PORTNUM 80
int GetRegionInfo(char *IP, char *RegionA, char *RegionB)
{
struct hostent *hptr;
int Sockfd=0, ret=0, timeout=0, tmplen=0;
char forSend[BUFLEN], forRecv[BUFLEN];
char *p, *p2;
struct sockaddr_in RemoteAddr;
int aaa=1;
if( strlen(IP) == 0 ){
// IP为空
return -1;
}
#ifdef WIN33
WSADATA WSAData;
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
printf("Network Initialization failed\n");
return -2;
}
#endif
//获取域名对应的服务器IP失败
if( (hptr = gethostbyname(HOSTDOMAIN)) == NULL){
printf("Get IP for %s Failed", HOSTDOMAIN);
return -3;
}
if( hptr->h_addr_list == NULL || hptr->h_addrtype != AF_INET)
return -3;
//初始化socket
Sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset( &RemoteAddr, 0, sizeof(RemoteAddr) );
RemoteAddr.sin_family = AF_INET;
RemoteAddr.sin_port = htons(PORTNUM);
RemoteAddr.sin_addr = *(struct in_addr *) hptr->h_addr;
//连接服务器
ret=connect(Sockfd, (struct sockaddr*)&RemoteAddr, sizeof(struct sockaddr));
if( ret < 0 ){
printf("connect to %s fail ", HOSTDOMAIN);
return -4;
}
memset( &forSend, 0, 1024 );
sprintf(forSend, "GET %s%s HTTP/1.1\n", URLPATH, IP);
sprintf(forSend, "%sAccept: txt/html\nReferer: http://%s/\n", forSend, HOSTDOMAIN);
sprintf(forSend, "%sAccept-Language: zh-CN\nUser-Agent: Mozilla/4.0\n", forSend);
sprintf(forSend, "%sHost: %s\nConnection: Keep-Alive\n\n\n\n", forSend, HOSTDOMAIN);
//发送头信息
ret=send(Sockfd, forSend, strlen(forSend), 0);
if (ret<0)
{
printf("send http header failed\n");
return -5;
}
//收取信息
while(1){
memset( forRecv, 0, sizeof(forRecv) );
ret=recv( Sockfd, forRecv, BUFLEN, 0 );
if ( ret < 1 )
{
if ( timeout++ > 5 )
{
printf("try too much time to recv, forget it\n");
#ifdef WIN32
closesocket(Sockfd);
#else
close(Sockfd);
#endif
return -6;
}
#ifdef WIN32
Sleep(1);
#else
sleep(1);
#endif
continue;
}
else{
p=strstr(forRecv, "本站主数据");
if ( p )
{
aaa=2;
memset(RegionA, 0, BUFLEN);
p=p+strlen("本站主数据:");
p2=strstr(p, "</li>");
if (p2)
{
tmplen=strlen(p)-strlen(p2);
memcpy(RegionA, p, tmplen);
if (tmplen<BUFLEN)
{
RegionA[tmplen]='\0';
}
}
}
p=strstr(forRecv, "参考数据一:");
if ( p )
{
aaa=3;
memset(RegionB, 0, BUFLEN);
p=p+strlen("参考数据一:");
p2=strstr(p, "</li>");
if(p2){
tmplen=strlen(p)-strlen(p2);
memcpy(RegionB, p, tmplen);
if (tmplen<BUFLEN)
{
RegionB[tmplen]='\0';
}
break;
}
}
p=strstr(forRecv, "</html>");
if (p)
break;
}
}
#ifdef WIN32
closesocket(Sockfd);
#else
close(Sockfd);
#endif
return 0;
}
int main(int argc, char **argv){
int i=0;
char *IPLIST[4]={"11.30.255.92","12.31.56.141","12.35.60.255","24.31.202.50"};
char RegionA[BUFLEN], RegionB[BUFLEN];
for(i=0; i<4; i++){
GetRegionInfo(IPLIST[i], RegionA, RegionB);
printf("Result:\n %s\n %s\n", RegionA, RegionB);
}
return 0;
}