当前位置: 技术问答>linux和unix
请教 ping 程序问题
来源: 互联网 发布时间:2015-11-19
本文导语: 自己写了个PING 程序 环境 FC3, GCC编译 用 ping 命令别的机器和自己机器都可以正常ping 通 我的 程序ping 自己机器正常 ping 别的机器 sendto 成功但是 recvfrom 返回都是-1, 各位高手可以给点提示吗? 我把初始化...
自己写了个PING 程序
环境 FC3, GCC编译
用 ping 命令别的机器和自己机器都可以正常ping 通
我的 程序ping 自己机器正常
ping 别的机器 sendto 成功但是 recvfrom 返回都是-1,
各位高手可以给点提示吗?
我把初始化套接字 和收发的关键部分代码贴出来:
//Tony 20060411 FOR SUNIX//
//PING Version: V1.00
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MSG_BUF_SIZE 256
#define HOST_BUF_SIZE 128
struct sockaddr_in dest,send_addr;
int sockfd;
int pid;
int datalen=56;
char hostname[HOST_BUF_SIZE];
char message_buf[MSG_BUF_SIZE];
int seq=0;
static int Pingsocket=0;//tem
// some functions will be used
void handle_singal(int);
void handle_tv(struct timeval *,struct timeval *);
unsigned short check_sum(unsigned short *,int);
void handle_icmp(char *,int);
void recv_icmp(void);
void send_icmp(struct sockaddr_in);
void handle_signal(int);
void stat();
int main(int argc,char *argv[])
{
int n;
int flags;
struct hostent *he;
struct sigaction action;
if(argc!=2)
{
fprintf(stderr,"USAGE:ping host!n");//提示参数不全
exit(1);
}
action.sa_handler=handle_signal;
sigemptyset(&action.sa_mask);
action.sa_flags=0;
// 为了定时发送icmp包所作的 信号
sigaction(SIGALRM,&action,NULL);
/* get pid of 'main' to set the flag of ICMP */
pid=getpid();
dest.sin_family=AF_INET;
// use inet_aton() making IP_address(argv[1]) to binary
if(inet_aton(argv[1],&dest.sin_addr)==1)
{
strcpy(hostname,argv[1]);
}
// if argv[1] is not IP_address
else
{
// get info by host_name
he=gethostbyname(argv[1]);
if(he!=NULL)//ok
{
dest.sin_addr=*(struct in_addr *)he->h_addr[0];
strcpy(hostname,he->h_name);
exit(1);
}
else
{
fprintf(stderr,"host name error:%s%sn",argv[1],hstrerror(h_errno));
exit(1);
}
}
printf("PING %s(%s):n",hostname,inet_ntoa(dest.sin_addr));
// create a socket
sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(sockfdtype=ICMP_ECHO;
icmp->code=0;
icmp->un.echo.sequence=seq++;
icmp->un.echo.id=pid;//
struct timeval *a=(struct timeval *)(icmp+1);
gettimeofday(a,NULL); //calc process time
len=datalen+8;
icmp->checksum=0;
icmp->checksum=check_sum((u_short *)icmp,len);
//发送
len=sendto(sockfd,(char *)message_buf,len,0,(struct sockaddr *)&send_addr,sizeof(send_addr));
//rc = sendto(s, &icpmhdr, sizeof(struct icmphdr), 0, (struct sockaddr*)&addr, sizeof(addr));
//printf("1111111len:%dn", len);
if(len
环境 FC3, GCC编译
用 ping 命令别的机器和自己机器都可以正常ping 通
我的 程序ping 自己机器正常
ping 别的机器 sendto 成功但是 recvfrom 返回都是-1,
各位高手可以给点提示吗?
我把初始化套接字 和收发的关键部分代码贴出来:
//Tony 20060411 FOR SUNIX//
//PING Version: V1.00
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MSG_BUF_SIZE 256
#define HOST_BUF_SIZE 128
struct sockaddr_in dest,send_addr;
int sockfd;
int pid;
int datalen=56;
char hostname[HOST_BUF_SIZE];
char message_buf[MSG_BUF_SIZE];
int seq=0;
static int Pingsocket=0;//tem
// some functions will be used
void handle_singal(int);
void handle_tv(struct timeval *,struct timeval *);
unsigned short check_sum(unsigned short *,int);
void handle_icmp(char *,int);
void recv_icmp(void);
void send_icmp(struct sockaddr_in);
void handle_signal(int);
void stat();
int main(int argc,char *argv[])
{
int n;
int flags;
struct hostent *he;
struct sigaction action;
if(argc!=2)
{
fprintf(stderr,"USAGE:ping host!n");//提示参数不全
exit(1);
}
action.sa_handler=handle_signal;
sigemptyset(&action.sa_mask);
action.sa_flags=0;
// 为了定时发送icmp包所作的 信号
sigaction(SIGALRM,&action,NULL);
/* get pid of 'main' to set the flag of ICMP */
pid=getpid();
dest.sin_family=AF_INET;
// use inet_aton() making IP_address(argv[1]) to binary
if(inet_aton(argv[1],&dest.sin_addr)==1)
{
strcpy(hostname,argv[1]);
}
// if argv[1] is not IP_address
else
{
// get info by host_name
he=gethostbyname(argv[1]);
if(he!=NULL)//ok
{
dest.sin_addr=*(struct in_addr *)he->h_addr[0];
strcpy(hostname,he->h_name);
exit(1);
}
else
{
fprintf(stderr,"host name error:%s%sn",argv[1],hstrerror(h_errno));
exit(1);
}
}
printf("PING %s(%s):n",hostname,inet_ntoa(dest.sin_addr));
// create a socket
sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(sockfdtype=ICMP_ECHO;
icmp->code=0;
icmp->un.echo.sequence=seq++;
icmp->un.echo.id=pid;//
struct timeval *a=(struct timeval *)(icmp+1);
gettimeofday(a,NULL); //calc process time
len=datalen+8;
icmp->checksum=0;
icmp->checksum=check_sum((u_short *)icmp,len);
//发送
len=sendto(sockfd,(char *)message_buf,len,0,(struct sockaddr *)&send_addr,sizeof(send_addr));
//rc = sendto(s, &icpmhdr, sizeof(struct icmphdr), 0, (struct sockaddr*)&addr, sizeof(addr));
//printf("1111111len:%dn", len);
if(len