当前位置: 技术问答>unix/linux知识
iis7站长之家
请假高手PING的问题
来源: 互联网 发布时间:2016-07-17
本文导语: 从网上抄了一段ping的代码,单独测试没有问题,同时多ping几个也是可以的,比如5、6个,但当同时ping很多(50个以上)的时候,就会出现问题,应该是不通的网络ping的结果是通的,请各位高手多多指点! 逻辑: ...
从网上抄了一段ping的代码,单独测试没有问题,同时多ping几个也是可以的,比如5、6个,但当同时ping很多(50个以上)的时候,就会出现问题,应该是不通的网络ping的结果是通的,请各位高手多多指点!
逻辑:
得到ping的网络的个数n,如50
开n个线程同时对网址进行ping,范围是局域网
程序代码如下:
int CPing::ping( char *ips, int timeout)
{
unsigned long inaddr=0l;
struct hostent *host;
struct sockaddr_in addr;
struct timeval recvtime;//接收时间
struct timeval different;//包的经过时间
bzero(&addr,sizeof(addr));
addr.sin_family=AF_INET;
/*判断是主机名还是ip地址*/
if( (inaddr=inet_addr(ips))==INADDR_NONE)
{
if((host=gethostbyname(ips) )==NULL) /*是主机名*/
{
return 0;
}
memcpy( &addr.sin_addr,host->h_addr,host->h_length);
}
else /*是ip地址*/
addr.sin_addr.s_addr = inet_addr(ips);
struct timeval timeo;
int sockfd;
struct sockaddr_in from;
struct timeval *tval;
struct ip *iph;
struct icmp *icmp;
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int n;
pid_t pid;
int maxfds = 0;
fd_set readfds;
// 取得socket
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd icmp_type=ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_cksum=0;
icmp->icmp_seq=0;
icmp->icmp_id=pid;
packsize=8+56;
tval= (struct timeval *)icmp->icmp_data;//发送包的时间
gettimeofday(tval,NULL);
icmp->icmp_cksum=cal_chksum((unsigned short *)icmp,packsize);
// 发包
n = sendto(sockfd, (char *)&sendpacket, packsize, 0, (struct sockaddr *)&addr, sizeof(addr));
if (n icmp_id);
// 判断Ping回复包的状态
if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == pid))
{
// 正常就退出循环
tval=(struct timeval *)icmp->icmp_data;
different.tv_sec = recvtime.tv_sec - tval->tv_sec;
different.tv_usec = recvtime.tv_usec - tval->tv_usec;
spenttime=(different.tv_sec)*1000.0 + (different.tv_usec)/1000.0;
//printf("time=%1.4fmsn",(different.tv_sec)*1000.0 + (different.tv_usec)/1000.0);
close(sockfd);
return 1;
}
else
{
continue;
}
}
// 关闭socket
close(sockfd);
}
逻辑:
得到ping的网络的个数n,如50
开n个线程同时对网址进行ping,范围是局域网
程序代码如下:
int CPing::ping( char *ips, int timeout)
{
unsigned long inaddr=0l;
struct hostent *host;
struct sockaddr_in addr;
struct timeval recvtime;//接收时间
struct timeval different;//包的经过时间
bzero(&addr,sizeof(addr));
addr.sin_family=AF_INET;
/*判断是主机名还是ip地址*/
if( (inaddr=inet_addr(ips))==INADDR_NONE)
{
if((host=gethostbyname(ips) )==NULL) /*是主机名*/
{
return 0;
}
memcpy( &addr.sin_addr,host->h_addr,host->h_length);
}
else /*是ip地址*/
addr.sin_addr.s_addr = inet_addr(ips);
struct timeval timeo;
int sockfd;
struct sockaddr_in from;
struct timeval *tval;
struct ip *iph;
struct icmp *icmp;
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int n;
pid_t pid;
int maxfds = 0;
fd_set readfds;
// 取得socket
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd icmp_type=ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_cksum=0;
icmp->icmp_seq=0;
icmp->icmp_id=pid;
packsize=8+56;
tval= (struct timeval *)icmp->icmp_data;//发送包的时间
gettimeofday(tval,NULL);
icmp->icmp_cksum=cal_chksum((unsigned short *)icmp,packsize);
// 发包
n = sendto(sockfd, (char *)&sendpacket, packsize, 0, (struct sockaddr *)&addr, sizeof(addr));
if (n icmp_id);
// 判断Ping回复包的状态
if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == pid))
{
// 正常就退出循环
tval=(struct timeval *)icmp->icmp_data;
different.tv_sec = recvtime.tv_sec - tval->tv_sec;
different.tv_usec = recvtime.tv_usec - tval->tv_usec;
spenttime=(different.tv_sec)*1000.0 + (different.tv_usec)/1000.0;
//printf("time=%1.4fmsn",(different.tv_sec)*1000.0 + (different.tv_usec)/1000.0);
close(sockfd);
return 1;
}
else
{
continue;
}
}
// 关闭socket
close(sockfd);
}
|
// 判断是否是自己Ping的回复
// syslog(LOG_INFO,"fomr ip:%s",from_ip);
char *from_ip = (char *)inet_ntoa(from.sin_addr);
char *local_ip = (char *)inet_ntoa(addr.sin_addr);
if( strcmp(from_ip,local_ip)!=0 )
{
continue;
}
不是很了解icmp协议,还会收到不同到ip回来到包的吗?
不过这个 strcmp( 很有问题啊,会不会导致 192.0.0.1 和 192.0.0.11 一样到结果呢。
你还不如直接 from.sin_addr == addr.sin_addr 判断好了,还比你strcmp高效一些
// syslog(LOG_INFO,"fomr ip:%s",from_ip);
char *from_ip = (char *)inet_ntoa(from.sin_addr);
char *local_ip = (char *)inet_ntoa(addr.sin_addr);
if( strcmp(from_ip,local_ip)!=0 )
{
continue;
}
不是很了解icmp协议,还会收到不同到ip回来到包的吗?
不过这个 strcmp( 很有问题啊,会不会导致 192.0.0.1 和 192.0.0.11 一样到结果呢。
你还不如直接 from.sin_addr == addr.sin_addr 判断好了,还比你strcmp高效一些