问:linux下实现ping lost 100% 没找到原因 急!
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 < nsend)
(gdb) s
138 alarm(MAX_WAIT_TIME);
(gdb) s
141 if((n = recvfrom(sockfd,recvpacket,sizeof(recvpacket),0,(struct sockaddr *)&from,&fromlen)) < 0)
(gdb) s
-------------------PING statistics----------------
3 packets transmitted,0 received,100% lost,time 0 ms
rtt min/avg/max/mdev = 0.000/-nan/0.000/-nan ms
[Inferior 1 (process 14069) exited with code 01]
warning: Error removing breakpoint 0
warning: Error removing breakpoint 0
warning: Error removing breakpoint 0
在recvfrom的时候直接退出了 不知道为什么 而且还是exit1
跟网上别人的代码比了比 好像没什么区别啊
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <setjmp.h>
#include <errno.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd,datalen = 56;
int nsend = 0, nreceived = 0; //发送、接收包的数量
double temp_rtt[MAX_NO_PACKETS]; //记录每组收发包的往返时间
double all_time = 0,min = 0,max = 0,avg = 0,mdev = 0;
struct sockaddr_in dest_addr;
struct sockaddr_in from;
struct timeval tvrecv;
pid_t pid; //表示进程id
void statistics(int signo);
void send_packet(void);
void recv_packet(void);
void computer_rtt(void);
void tv_sub(struct timeval *out,struct timeval *in);
int pack(int pack_no);
int unpack(char *buf,int len);
unsigned short cal_chksum(unsigned short *addr, int len);
void computer_rtt() //计算rtt最小、大值,平均值,平均差
{
double sum_avg = 0;
int i;
min = max = temp_rtt[0];
avg =&