当前位置: 技术问答>linux和unix
问:linux下实现ping lost 100% 没找到原因 急!
来源: 互联网 发布时间:2017-05-13
本文导语: se1012@ubuntu:~/Desktop/ping$ ./myping www.sina.com PING www.sina.com(202.108.33.60):56 bytes of data. -------------------PING statistics---------------- 3 packets transmitted,0 received,100% lost,time 0 ms rtt min/avg/max/mdev = 0.000/-nan/0.000/-nan ms 不太会...
se1012@ubuntu:~/Desktop/ping$ ./myping www.sina.com
PING www.sina.com(202.108.33.60):56 bytes of data.
-------------------PING statistics----------------
3 packets transmitted,0 received,100% lost,time 0 ms
rtt min/avg/max/mdev = 0.000/-nan/0.000/-nan ms
不太会用Linux 用gdb看了一下
(gdb) set args www.sina.com
(gdb) b 129
Breakpoint 1 at 0x8048b10: file ping.c, line 129.
(gdb) r
Starting program: /home/se1012/Desktop/ping/myping www.sina.com
PING www.sina.com(202.108.33.60):56 bytes of data.
s
Breakpoint 1, recv_packet () at ping.c:133
133 fromlen = sizeof(from);
(gdb) s
134 signal(SIGALRM,statistics);
(gdb) s
136 while(nreceived > 16); //把进位的值也加到校验和上
answer = ~sum; //取反得到校验和
return answer;
}
int pack(int pack_no) //设置ICMP报头
{
int i,packsize;
struct icmp *icmp;
//struct timeval *tval;
icmp = (struct icmp*)sendpacket; //将sendpacket强制转换成ICMP格式
icmp->icmp_type = ICMP_ECHO; //ICMP_ECHO类型的类型号为0
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_seq = pack_no; //ICMP报文的发送顺序
icmp->icmp_id = pid;
packsize = 8 + datalen; //数据报大小为64字节
//tval = (struct timeval *)icmp->icmp_data;
//gettimeofday(tval,NULL); //记录发送时间
//校验算法
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp,packsize);
return packsize;
}
//发送3个ICMP报文
void send_packet()
{
int packetsize;
while(nsend tv_sec - sendtime->tv_sec;
long usec = recvtime->tv_usec - sendtime->tv_usec;
if(usec >= 0){
recvtime->tv_sec = sec;
recvtime->tv_usec = usec;
}else{ //接受时间usec小于发送时间usec
recvtime->tv_sec = sec - 1;
recvtime->tv_usec =1000000+usec;
}
}
int main(int argc,char *argv[]) //主函数
{
struct hostent *host=NULL; //host entry,记录主机信息
struct protoent *protocol=NULL; //
unsigned long inaddr = 0;
int waittime = MAX_WAIT_TIME;
int size = 50 * 1024;
//参数小于两个
if(argc p_proto)) h_addr,host->h_length);
}
else{ //是IP地址
dest_addr.sin_addr.s_addr = inaddr; /*or memcpy((char *)&dest_addr.sin_addr,argv[1],strlen(argv[1])+1);*/
}
//获取main的进程id,用于设置ICMP的标志符
pid = getpid();
printf("PING %s(%s):%d bytes of data.n",argv[1],inet_ntoa(dest_addr.sin_addr),datalen);
send_packet(); /*发送所有ICMP报文*/
recv_packet(); /*接收所有ICMP报文*/
statistics(SIGALRM); /*进行统计*/
return 0;
}
PING www.sina.com(202.108.33.60):56 bytes of data.
-------------------PING statistics----------------
3 packets transmitted,0 received,100% lost,time 0 ms
rtt min/avg/max/mdev = 0.000/-nan/0.000/-nan ms
不太会用Linux 用gdb看了一下
(gdb) set args www.sina.com
(gdb) b 129
Breakpoint 1 at 0x8048b10: file ping.c, line 129.
(gdb) r
Starting program: /home/se1012/Desktop/ping/myping www.sina.com
PING www.sina.com(202.108.33.60):56 bytes of data.
s
Breakpoint 1, recv_packet () at ping.c:133
133 fromlen = sizeof(from);
(gdb) s
134 signal(SIGALRM,statistics);
(gdb) s
136 while(nreceived > 16); //把进位的值也加到校验和上
answer = ~sum; //取反得到校验和
return answer;
}
int pack(int pack_no) //设置ICMP报头
{
int i,packsize;
struct icmp *icmp;
//struct timeval *tval;
icmp = (struct icmp*)sendpacket; //将sendpacket强制转换成ICMP格式
icmp->icmp_type = ICMP_ECHO; //ICMP_ECHO类型的类型号为0
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_seq = pack_no; //ICMP报文的发送顺序
icmp->icmp_id = pid;
packsize = 8 + datalen; //数据报大小为64字节
//tval = (struct timeval *)icmp->icmp_data;
//gettimeofday(tval,NULL); //记录发送时间
//校验算法
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp,packsize);
return packsize;
}
//发送3个ICMP报文
void send_packet()
{
int packetsize;
while(nsend tv_sec - sendtime->tv_sec;
long usec = recvtime->tv_usec - sendtime->tv_usec;
if(usec >= 0){
recvtime->tv_sec = sec;
recvtime->tv_usec = usec;
}else{ //接受时间usec小于发送时间usec
recvtime->tv_sec = sec - 1;
recvtime->tv_usec =1000000+usec;
}
}
int main(int argc,char *argv[]) //主函数
{
struct hostent *host=NULL; //host entry,记录主机信息
struct protoent *protocol=NULL; //
unsigned long inaddr = 0;
int waittime = MAX_WAIT_TIME;
int size = 50 * 1024;
//参数小于两个
if(argc p_proto)) h_addr,host->h_length);
}
else{ //是IP地址
dest_addr.sin_addr.s_addr = inaddr; /*or memcpy((char *)&dest_addr.sin_addr,argv[1],strlen(argv[1])+1);*/
}
//获取main的进程id,用于设置ICMP的标志符
pid = getpid();
printf("PING %s(%s):%d bytes of data.n",argv[1],inet_ntoa(dest_addr.sin_addr),datalen);
send_packet(); /*发送所有ICMP报文*/
recv_packet(); /*接收所有ICMP报文*/
statistics(SIGALRM); /*进行统计*/
return 0;
}
|
main 函数里面if(inaddr=inet_addr(argv[1]) == INADDR_NONE)
if((inaddr=inet_addr(argv[1])) == INADDR_NONE)
if((inaddr=inet_addr(argv[1])) == INADDR_NONE)
|
那必然是虚拟机设置的问题了