分享
 
 
 

如何用winsock实现ping(源代码)

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

/*This is a sample routine of ping.It's implemented with winsock1.1

* under windows 2000 professional and has been not tested under other

* platform.Our target is to illustrator the principles,so many details

* may be ignored.(author email:zhangwu2003@163.com)

*/

#include<stdio.h>

#include<windows.h>

#include<process.h>

#define SEND_SIZE 32

#define PACKET_SIZE 4096

#define ICMP_ECHO 8

#define ICMP_ECHOREPLY 0

struct icmp

{

unsigned char icmp_type;

unsigned char icmp_code;

unsigned short icmp_cksum;

unsigned short icmp_id;

unsigned short icmp_seq;

unsigned long icmp_data;

};

struct ip

{

unsigned char ip_hl:4;

unsigned char ip_v:4;

unsigned char ip_tos;

unsigned short ip_len;

unsigned short ip_id;

unsigned short ip_off;

unsigned char ip_ttl;

unsigned char ip_p;

unsigned short ip_sum;

unsigned long ip_src;

unsigned long ip_dst;

};

unsigned char sendpacket[PACKET_SIZE];

unsigned char recvpacket[PACKET_SIZE];

struct sockaddr_in dest_addr;

struct sockaddr_in from_addr;

int sockfd;

int pid;

unsigned short cal_chksum(unsigned short *addr,int len);

int pack(int pack_no);

int unpack(unsigned char *buf,int len);

void send_packet(void);

void recv_packet(void);

void main(int argc,char *argv[])

{

struct hostent *host;

struct protoent *protocol;

WSADATA wsaData;

int timeout=1000;

int SEND_COUNT=4;

int i;

char *par_host;

par_host=argv[argc-1];

switch(argc)

{

case 2: break;

case 3: if(strcmp(argv[1],"-t")==0)

{

SEND_COUNT=10000;

break;

}

//fall through

default:

printf("usage: %s [-t] Host name or IP address\n",argv[0]);

exit(1);

}

if(WSAStartup(0x1010,&wsaData)!=0)

{

printf("wsastartup error\n");

exit(1);

}

if( (protocol=getprotobyname("icmp") )==NULL)

{

printf("getprotobyname error\n");

exit(1);

}

if( (sockfd=socket(AF_INET,SOCK_RAW,protocol->p_proto) )<0)

{

printf("socket error\n");

exit(1);

}

if(setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,sizeof(timeout))<0)

fprintf(stderr,"failed to set recv timeout: %d\n",WSAGetLastError());

if(setsockopt(sockfd,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,sizeof(timeout))<0)

fprintf(stderr,"failed to set send timeout: %d\n",WSAGetLastError());

memset(&dest_addr,0,sizeof(dest_addr));

dest_addr.sin_family=AF_INET;

if(host=gethostbyname(par_host) )

{

memcpy( (char *)&dest_addr.sin_addr,host->h_addr,host->h_length);

//resolve address to hostname

if(host=gethostbyaddr(host->h_addr,4,PF_INET))

par_host=host->h_name;

}

else if( dest_addr.sin_addr.s_addr=inet_addr(par_host)==INADDR_NONE)

{

printf("Unkown host %s\n",par_host);

exit(1);

}

pid=getpid();

printf("Pinging %s [%s]: with %d bytes of data:\n\n",par_host,inet_ntoa(dest_addr.sin_addr),SEND_SIZE);

for(i=0;i<SEND_COUNT;i++)

{

send_packet();

recv_packet();

Sleep(1000);

}

}

//this algorithm is referenced from other's

unsigned short cal_chksum(unsigned short *addr,int len)

{

int nleft=len;

int sum=0;

unsigned short *w=addr;

unsigned short answer=0;

while(nleft>1)

{ sum+=*w++;

nleft-=2;

}

if( nleft==1)

{ *(unsigned char *)(&answer)=*(unsigned char *)w;

sum+=answer;

}

sum=(sum>>16)+(sum&0xffff);

sum+=(sum>>16);

answer=~sum;

return answer;

}

//打包

int pack(int pack_no)

{

int packsize;

struct icmp *icmp;

packsize=8+SEND_SIZE;

icmp=(struct icmp*)sendpacket;

icmp->icmp_type=ICMP_ECHO;

icmp->icmp_code=0;

icmp->icmp_cksum=0;

icmp->icmp_seq=pack_no;

icmp->icmp_id=pid;

icmp->icmp_data=GetTickCount();

icmp->icmp_cksum=cal_chksum( (unsigned short *)icmp,packsize); /*校验算法*/

return packsize;

}

//解包

int unpack(unsigned char *buf,int len)

{

struct ip *ip;

struct icmp *icmp;

double rtt;

int iphdrlen;

ip=(struct ip *)buf;

iphdrlen=ip->ip_hl*4;

icmp=(struct icmp *)(buf+iphdrlen);

if( (icmp->icmp_type==ICMP_ECHOREPLY) && (icmp->icmp_id==pid) )

{

len=len-iphdrlen-8;

rtt=GetTickCount()-icmp->icmp_data;

printf("Reply from %s: bytes=%d time=%.0fms TTL=%d icmp_seq=%u\n",

inet_ntoa(from_addr.sin_addr),

len,

rtt,

ip->ip_ttl,

icmp->icmp_seq);

return 1;

}

return 0;

}

//发送

void send_packet()

{

int packetsize;

static int pack_no=0;

packetsize=pack(pack_no++);

if( sendto(sockfd,sendpacket,packetsize,0,(struct sockaddr *)&dest_addr,sizeof(dest_addr) )<0 )

printf("Destination host unreachable.\n");

// printf("send NO %d\n",pack_no-1);

}

//接收

void recv_packet()

{

int n,fromlen;

int success;

fromlen=sizeof(from_addr);

do

{

if( (n=recvfrom(sockfd,recvpacket,sizeof(recvpacket),0,(struct sockaddr *)&from_addr,&fromlen)) >=0)

success=unpack(recvpacket,n);

else if (WSAGetLastError() == WSAETIMEDOUT)

{

printf("Request timed out.\n");

return;

}

}while(!success);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有