亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用winsock實現ping

發布時間:2021-02-03 10:54:31 來源:億速云 閱讀:275 作者:小新 欄目:編程語言

小編給大家分享一下怎么用winsock實現ping,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

/*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);

}

看完了這篇文章,相信你對“怎么用winsock實現ping”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大埔县| 黔西县| 高邮市| 甘谷县| 朝阳区| 玉屏| 兴山县| 阳西县| 贵溪市| 黔西| 肇庆市| 彭泽县| 北宁市| 石屏县| 江都市| 德江县| 南和县| 南雄市| 巴马| 蒙阴县| 黄平县| 肃北| 广河县| 乌兰浩特市| 天等县| 施甸县| 新龙县| 手游| 玉龙| 祁阳县| 册亨县| 偃师市| 华安县| 乳源| 子洲县| 灌阳县| 泸州市| 东台市| 陵川县| 深圳市| 承德市|