#include #include #include #include //ip_ttl
#pragma comment(lib,"ws2_32.lib")
//icmp首部結構體
typedef struct tagicmpheader
icmpheader;
//ip首部結構體
typedef struct _ip_hdr
ipheader;
//計算檢驗和
unsigned short checksum(unsigned short *buffer, int size)
if (size) //加上最後乙個byte
while (cksum>>16)
return (unsigned short)~cksum;}
int main()
//設定icmp首部內容
icmpheader icmp_hdr;
icmp_hdr.type=8;
icmp_hdr.code=0;
icmp_hdr.checknum=0;
icmp_hdr.id=(unsigned short)getcurrentprocessid();
icmp_hdr.seq_num=0;
//計算檢驗和
icmp_hdr.checknum=checksum((unsigned short*)&icmp_hdr,sizeof(icmp_hdr));
//設定原始套接字
socket sock=wsasocket(af_inet,sock_raw,ipproto_icmp,null,0,0);
if(invalid_socket==sock)
int ttl=255;
//設定ttl為255
if(setsockopt(sock,ipproto_ip,ip_ttl,(const char*)&ttl,sizeof(ttl))==socket_error)
//目標位址
sockaddr_in dest_addr;
memset(&dest_addr,0,sizeof(dest_addr));
dest_addr.sin_family=af_inet;
dest_addr.sin_addr.s_un.s_addr=inet_addr("192.168.1.1");
//傳送icmp請求報文
int status=sendto(sock,(const char*)&icmp_hdr,sizeof(icmp_hdr),0,(sockaddr*)&dest_addr,sizeof(dest_addr));
if(socket_error==status)
int size=60+sizeof(icmpheader);
char *buffer=(char*)malloc(size);
memset(buffer,0,size);
int des=sizeof(sockaddr_in);
//接受icmp回應報文並分析
while (true)
}else
}shutdown(sock,sd_both);
closesocket(sock);
wsacleanup();
return 0;
}
原始套接字之PING 2
主函式 ping.c include include include include include include include include include include include bzero include include 儲存已經傳送包的狀態值 typedef struct pi...
使用原始套接字程式設計實現簡單的ping程式
程式實現步驟 1 初始化windows sockets網路環境 wsadata wsa wsastartup makeword 2,2 wsa 2 構造目的端socket位址 3 建立原始套接字 4 定義ip和icmp頭部資料結構 5 傳送報文 6 接收報文 其中icmp回顯請求與回顯應答報文結構如...
vc 6 0 除錯程式
之前一直不太會用vc 6.0,很多時候程式一出錯就不知道怎麼除錯了,相信學會設定斷點 單步除錯後,以後可以理解更多程式背後的事情,至少有了自己去探索研究的工具和方法,特此總結下 在vc 6.0中的單步除錯 除錯重要的幾個鍵 f9在當前游標所在的行下斷點,如果當前行已經有斷點,則取消斷點.f5除錯狀態...