青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 45  文章 - 129  trackbacks - 0
<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

專注于C++ P2P STL GP OpenSource等
Google

常用鏈接

留言簿(10)

隨筆分類

隨筆檔案

相冊

朋友

  • .NET

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

作者:侯志江

編寫自己的一個ping程序,可以說是許多人邁出網絡編程的第一步吧!!這個ping程序的源代碼經過我的修改和調試,基本上可以取代windows中自帶的ping程序. 各個模塊后都有我的詳細注釋和修改日志,希望能夠對大家的學習有所幫助!!

/*? 本程序的主要源代碼來自MSDN網站, 筆者只是做了一些改進和注釋! 另外需要注意的是在Build之前,必須加入ws2_32.lib庫文件,否則會提示"error LNK2001:"的錯誤!*/

/******************************************************************************\
| Version 1.1 修改記錄:????????????????????????????????????????????????????????????????????????????????????????????????????????????? |
|??? <1> 解決了socket阻塞的問題,從而能夠正確地處理超時的請求!??????????????????????????????????????????????????????? |
|----------------------------------------------------------------------------------------------------|
| Version 1.2 修改記錄:????????????????????????????????????????????????????????????????????????????????????????????????????????????? |
|??? <1> 增加了由用戶控制發送ICMP包的數目的功能(即命令的第二個參數).????????????????????????????????????????????? |???
|??? <2> 增加了對ping結果的統計功能.?????????????????????????????????????????????????????????????????????????????????????????? |
\******************************************************************************/

#pragma pack(4)

#include
#include
#include

#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0

#define ICMP_MIN 8 // minimum 8 byte icmp packet (just header)

/* The IP header */
typedef struct iphdr {
unsigned int h_len:4; // length of the header
unsigned int version:4; // Version of IP
unsigned char tos; // Type of service
unsigned short total_len; // total length of the packet
unsigned short ident; // unique identifier
unsigned short frag_and_flags; // flags
unsigned char ttl;
unsigned char proto; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum

unsigned int sourceIP;
unsigned int destIP;

}IpHeader;

//
// ICMP header
//
typedef struct icmphdr {
BYTE i_type;
BYTE i_code; /* type sub code */
USHORT i_cksum;
USHORT i_id;
USHORT i_seq;
/* This is not the std header, but we reserve space for time */
ULONG timestamp;
}IcmpHeader;

#define STATUS_FAILED 0xFFFF
#define DEF_PACKET_SIZE??? 32
#define DEF_PACKET_NUMBER? 4??? /* 發送數據報的個數 */
#define MAX_PACKET 1024

#define xmalloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))
#define xfree(p) HeapFree (GetProcessHeap(),0,(p))

void fill_icmp_data(char *, int);
USHORT checksum(USHORT *, int);
int decode_resp(char *,int ,struct sockaddr_in *);

void Usage(char *progname){

fprintf(stderr,"Usage:\n");
fprintf(stderr,"%s [number of packets] [data_size]\n",progname);
fprintf(stderr,"datasize can be up to 1Kb\n");
ExitProcess(STATUS_FAILED);

}
int main(int argc, char **argv){

WSADATA wsaData;
SOCKET sockRaw;
struct sockaddr_in dest,from;
struct hostent * hp;
int bread,datasize,times;
int fromlen = sizeof(from);
int timeout = 1000;
int statistic = 0;? /* 用于統計結果 */?
char *dest_ip;
char *icmp_data;
char *recvbuf;
unsigned int addr=0;
USHORT seq_no = 0;

if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0){
fprintf(stderr,"WSAStartup failed: %d\n",GetLastError());
ExitProcess(STATUS_FAILED);
}

if (argc <2 ) {
Usage(argv[0]);
}
sockRaw = WSASocket(AF_INET,SOCK_RAW,IPPROTO_ICMP,NULL, 0,WSA_FLAG_OVERLAPPED);

//
//注:為了使用發送接收超時設置(即設置SO_RCVTIMEO, SO_SNDTIMEO),
//??? 必須將標志位設為WSA_FLAG_OVERLAPPED !
//

if (sockRaw == INVALID_SOCKET) {
fprintf(stderr,"WSASocket() failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
bread = setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,
sizeof(timeout));
if(bread == SOCKET_ERROR) {
fprintf(stderr,"failed to set recv timeout: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
timeout = 1000;
bread = setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,
sizeof(timeout));
if(bread == SOCKET_ERROR) {
fprintf(stderr,"failed to set send timeout: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
memset(&dest,0,sizeof(dest));

hp = gethostbyname(argv[1]);

if (!hp){
addr = inet_addr(argv[1]);
}
if ((!hp) && (addr == INADDR_NONE) ) {
fprintf(stderr,"Unable to resolve %s\n",argv[1]);
ExitProcess(STATUS_FAILED);
}

if (hp != NULL)
memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
else
dest.sin_addr.s_addr = addr;

if (hp)
dest.sin_family = hp->h_addrtype;
else
dest.sin_family = AF_INET;

dest_ip = inet_ntoa(dest.sin_addr);

//
//? atoi函數原型是: int atoi( const char *string );
//? The return value is 0 if the input cannot be converted to an integer !
//
if(argc>2)
{
?times=atoi(argv[2]);
?if(times == 0)
? times=DEF_PACKET_NUMBER;
}
else
??? times=DEF_PACKET_NUMBER;

if (argc >3)
{
?datasize = atoi(argv[3]);
??? if (datasize == 0)
??????? datasize = DEF_PACKET_SIZE;
?if (datasize >1024)?? /* 用戶給出的數據包大小太大 */
?{
? fprintf(stderr,"WARNING : data_size is too large !\n");
? datasize = DEF_PACKET_SIZE;
?}
}
else
??? datasize = DEF_PACKET_SIZE;

datasize += sizeof(IcmpHeader);

icmp_data = (char*)xmalloc(MAX_PACKET);
recvbuf = (char*)xmalloc(MAX_PACKET);

if (!icmp_data) {
fprintf(stderr,"HeapAlloc failed %d\n",GetLastError());
ExitProcess(STATUS_FAILED);
}


memset(icmp_data,0,MAX_PACKET);
fill_icmp_data(icmp_data,datasize);

//
//顯示提示信息
//
fprintf(stdout,"\nPinging %s ....\n\n",dest_ip);


for(int i=0;i{
int bwrote;

((IcmpHeader*)icmp_data)->i_cksum = 0;
((IcmpHeader*)icmp_data)->timestamp = GetTickCount();

((IcmpHeader*)icmp_data)->i_seq = seq_no++;
((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data,datasize);

bwrote = sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)&dest,sizeof(dest));
if (bwrote == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("Request timed out.\n");
continue;
}
fprintf(stderr,"sendto failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
if (bwrote < datasize ) {
fprintf(stdout,"Wrote %d bytes\n",bwrote);
}
bread = recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(struct sockaddr*)&from,&fromlen);
if (bread == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("Request timed out.\n");
continue;
}
fprintf(stderr,"recvfrom failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
if(!decode_resp(recvbuf,bread,&from))
?statistic++; /* 成功接收的數目++ */
Sleep(1000);

}
?
/*
Display the statistic result
*/
fprintf(stdout,"\nPing statistics for %s \n",dest_ip);
fprintf(stdout,"??? Packets: Sent = %d,Received = %d, Lost = %d (%2.0f%% loss)\n",times,
???? statistic,(times-statistic),(float)(times-statistic)/times*100);


WSACleanup();
return 0;

}
/*
The response is an IP packet. We must decode the IP header to locate
the ICMP data
*/
int decode_resp(char *buf, int bytes,struct sockaddr_in *from) {

IpHeader *iphdr;
IcmpHeader *icmphdr;
unsigned short iphdrlen;

iphdr = (IpHeader *)buf;

iphdrlen = (iphdr->h_len) * 4 ; // number of 32-bit words *4 = bytes

if (bytes < iphdrlen + ICMP_MIN) {
printf("Too few bytes from %s\n",inet_ntoa(from->sin_addr));
}

icmphdr = (IcmpHeader*)(buf + iphdrlen);

if (icmphdr->i_type != ICMP_ECHOREPLY) {
fprintf(stderr,"non-echo type %d recvd\n",icmphdr->i_type);
return 1;
}
if (icmphdr->i_id != (USHORT)GetCurrentProcessId()) {
fprintf(stderr,"someone else's packet!\n");
return 1;
}
printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr));
printf(" icmp_seq = %d. ",icmphdr->i_seq);
printf(" time: %d ms ",GetTickCount()-icmphdr->timestamp);
printf("\n");
return 0;

}


USHORT checksum(USHORT *buffer, int size) {

unsigned long cksum=0;

while(size >1) {
cksum+=*buffer++;
size -=sizeof(USHORT);
}

if(size) {
cksum += *(UCHAR*)buffer;
}

cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
/*
Helper function to fill in various stuff in our ICMP request.
*/
void fill_icmp_data(char * icmp_data, int datasize){

IcmpHeader *icmp_hdr;
char *datapart;

icmp_hdr = (IcmpHeader*)icmp_data;

icmp_hdr->i_type = ICMP_ECHO;
icmp_hdr->i_code = 0;
icmp_hdr->i_id = (USHORT)GetCurrentProcessId();
icmp_hdr->i_cksum = 0;
icmp_hdr->i_seq = 0;

datapart = icmp_data + sizeof(IcmpHeader);
//
// Place some junk in the buffer.
//
memset(datapart,'E', datasize - sizeof(IcmpHeader));

}

/******************* 附: ping命令執行時顯示的畫面 ***************\
*? C:\Documents and Settings\houzhijiang>ping 236.56.54.12?????????????? *
*????????????????????????????????????????????????????????????????????????????????????????????????????? *
*? Pinging 236.56.54.12 with 32 bytes of data:????????????????????????????????????? *
*????????????????????????????????????????????????????????????????????????????????????????????????????? *
*? Request timed out.???????????????????????????????????????????????????????????????????????? *
*? Request timed out.???????????????????????????????????????????????????????????????????????? *
*? Request timed out.???????????????????????????????????????????????????????????????????????? *
*? Request timed out.???????????????????????????????????????????????????????????????????????? *
*????????????????????????????????????????????????????????????????????????????????????????????????????? *
*? Ping statistics for 236.56.54.12:????????????????????????????????????????????????????? *
*???? Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),????????????????? *
*????????????????????????????????????????????????????????????????????????????????????????????????????? *
\*********************************************************/

/*********************************************************\
*? C:\Documents and Settings\houzhijiang>ping 127.0.0.1??????????????????? *
*???????????????????????????????????????????????????????????????????????????????????????????????????? *
*? Pinging 127.0.0.1 with 32 bytes of data:????????????????????????????????????????? *
*???????????????????????????????????????????????????????????????????????????????????????????????????? *
*? Reply from 127.0.0.1: bytes=32 time<1ms TTL=128??????????????????????? *
*? Reply from 127.0.0.1: bytes=32 time<1ms TTL=128??????????????????????? *
*? Reply from 127.0.0.1: bytes=32 time<1ms TTL=128??????????????????????? *
*? Reply from 127.0.0.1: bytes=32 time<1ms TTL=128??????????????????????? *
*???????????????????????????????????????????????????????????????????????????????????????????????????? *
*? Ping statistics for 127.0.0.1:????????????????????????????????????????????????????????? *
*???? Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),??????????????????? *
*? Approximate round trip times in milli-seconds:???????????????????????????????? *
*???? Minimum = 0ms, Maximum = 0ms, Average = 0ms??????????????????????? *
*??????????????????????????????????????????????????????????????????????????????????????????????????? *
\********************************************************/

posted on 2007-01-05 13:46 CPP&&設計模式小屋 閱讀(1279) 評論(1)  編輯 收藏 引用 所屬分類: Network

FeedBack:
# re: 微軟ping程序源代碼完整版(轉)[未登錄] 2012-06-22 11:34 初學者
什么鳥玩意
既然把代碼貼出來
就應該把頭文件也給出了
搞飛機  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲免费在线| 久久九九电影| 亚洲欧洲日夜超级视频| 国产一区二区三区网站| 国产亚洲欧美色| 欧美亚洲一区| 欧美刺激性大交免费视频| 蜜桃视频一区| 正在播放欧美一区| 久久精品国产第一区二区三区最新章节| 久久久噜噜噜久噜久久| 欧美日本久久| 国产裸体写真av一区二区| 激情六月婷婷久久| 中日韩午夜理伦电影免费| 羞羞漫画18久久大片| 欧美一级网站| 欧美香蕉大胸在线视频观看| 一区免费观看| 亚洲精品乱码久久久久久日本蜜臀 | 欧美国产专区| 午夜精品一区二区三区电影天堂 | 久久久久91| 91久久中文| 久久久久一区| 亚洲摸下面视频| 一区二区三区亚洲| 亚洲久久在线| 欧美成年人视频| 韩日在线一区| 久久午夜羞羞影院免费观看| 亚洲一区中文字幕在线观看| 欧美色一级片| 亚洲网址在线| 日韩视频二区| 欧美日韩激情小视频| 亚洲免费av片| 欧美激情精品久久久久久黑人 | 欧美影院在线播放| 国产精品私人影院| 小嫩嫩精品导航| 欧美大成色www永久网站婷| 久久成人免费视频| 欧美亚洲一区二区在线| 亚洲国产精品久久久| 美女被久久久| 国产视频在线观看一区二区| 久久激情五月丁香伊人| 久久国产精品久久精品国产| 亚洲一区二区三区视频| 欧美国产一区二区在线观看| 亚洲精品资源| 久久免费高清视频| 亚洲欧洲精品一区二区三区波多野1战4| 亚洲永久字幕| 亚洲一区二区三区四区五区午夜| 欧美成人午夜激情视频| 欧美1区视频| 欧美日韩国产三区| 欧美黄色一区| 亚洲国产精品久久| 另类亚洲自拍| 亚洲性图久久| 欧美精品在线视频观看| 亚洲第一毛片| 欧美日韩网址| 久久精品首页| 国产亚洲va综合人人澡精品| 亚洲一区在线播放| 欧美与黑人午夜性猛交久久久| 久久伊人精品天天| 欧美11—12娇小xxxx| 亚洲国产成人精品女人久久久| 久久久久se| 欧美成人蜜桃| 亚洲裸体视频| 欧美色图天堂网| 亚洲视频免费看| 亚洲欧美视频在线观看| 久热精品视频在线观看一区| 乱码第一页成人| 国产精品久久国产精品99gif| 久久综合久色欧美综合狠狠| 激情久久久久| 模特精品裸拍一区| 久久激情五月丁香伊人| 黄色成人免费观看| 久热精品在线视频| 亚洲乱码精品一二三四区日韩在线 | 欧美裸体一区二区三区| 一区二区三区欧美成人| 在线成人www免费观看视频| 一区二区不卡在线视频 午夜欧美不卡在 | 国产一区二区黄色| 久久视频在线视频| 亚洲人成网站影音先锋播放| 亚洲欧美日韩成人| 欧美激情一级片一区二区| 久久久久在线| 亚洲精品一区在线观看| 国产精品大片| 久久亚洲国产成人| 亚洲乱码精品一二三四区日韩在线| 欧美一区二区三区在线免费观看| 曰韩精品一区二区| 欧美日韩天天操| 久久久久一区二区三区| 99这里有精品| 亚洲特级毛片| 亚洲国产精品久久| 国产精品网站在线观看| 欧美国产日产韩国视频| 亚洲免费在线视频| 日韩亚洲欧美精品| 久久亚洲精品中文字幕冲田杏梨| 一区二区国产精品| 欧美丰满高潮xxxx喷水动漫| 亚洲自拍高清| 日韩特黄影片| 欧美国产第一页| 久久久精品网| 一区二区三区在线高清| 欧美亚洲第一页| 欧美v国产在线一区二区三区| 亚洲一区二区精品| 亚洲欧洲一区二区三区在线观看 | 久久久国产91| 亚洲尤物在线视频观看| 国产精品白丝av嫩草影院| 久久婷婷麻豆| 久久国产精品久久久久久电车| 这里只有精品丝袜| 亚洲乱码国产乱码精品精| 欧美激情一区二区三区不卡| 久久久久久夜| 久久精品在线免费观看| 欧美一区二区三区播放老司机| 一区二区三区成人精品| 99国产精品久久久久久久| 国产精品国产三级国产| 欧美日韩国产二区| 欧美激情一区二区在线 | 亚洲视频精选在线| 亚洲美女电影在线| 亚洲黄色免费| 亚洲直播在线一区| 中国女人久久久| 亚洲一区网站| 日韩午夜电影在线观看| 亚洲精品一二| 一区电影在线观看| 亚洲永久免费| 欧美一区二区高清| 欧美一区二区三区喷汁尤物| 性欧美办公室18xxxxhd| 羞羞答答国产精品www一本| 亚洲欧美综合网| 亚洲人成7777| 亚洲美女中文字幕| 一区二区三区久久| 亚洲自拍高清| 久久久国产一区二区三区| 久久精品国产清高在天天线| 久久久精品一品道一区| 欧美搞黄网站| 国产精品99一区二区| 国产三级欧美三级| 依依成人综合视频| 亚洲日本视频| 国内精品久久久久影院薰衣草| 欧美成人有码| 国产精品久久久久久av福利软件| 国产日韩欧美亚洲| 亚洲高清电影| 亚洲视屏在线播放| 久久综合精品国产一区二区三区| 欧美成人按摩| 亚洲视频一区二区| 久久精品在线视频| 欧美日韩精品一区二区三区| 国产欧美三级| 欧美日韩专区在线| 国产在线拍揄自揄视频不卡99| 亚洲国产成人av好男人在线观看| 中文在线资源观看网站视频免费不卡| 欧美一级久久久久久久大片| 欧美电影在线| 午夜精品剧场| 欧美日韩午夜在线| 国产在线播精品第三| 一本色道久久综合精品竹菊| 91久久亚洲| 欧美制服丝袜第一页| 最新中文字幕亚洲| 久久久噜噜噜久久中文字幕色伊伊| 欧美午夜影院| 亚洲精品免费一二三区| 久久gogo国模啪啪人体图| 亚洲精品欧美日韩| 老司机免费视频久久|