資源簡介
計算機網絡課程設計,發現網絡中的活動主機。無需調用cmd,直接可在exe中輸出結果的程序、
代碼片段和文件信息
#pragma?pack(4)
#pragma?comment(lib“WS2_32.LIB“)
#define?WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
#include
#include
#includese.h>
//頭文件
typedef?struct?iphdr{??????????????????//IP頭
unsigned?int?headlen:4;????????????//IP頭長度
unsigned?int?version:4;????????????//IP版本號
unsigned?char?tos;?????????????????//服務類型
unsigned?short?id;?????????????????//ID號
unsigned?short?flag;???????????????//標記
unsigned?char?ttl;?????????????????//生存時間
unsigned?char?prot;????????????????//協議
unsigned?short?checksum;???????????//效驗和
unsigned?int?sourceIP;?????????????//源IP
unsigned?int?destIP;???????????????//目的IP
}IpHeader;
//IP頭部
typedef?struct?icmphdr{????????????????//ICMP頭
BYTE?type;?????????????????????????//ICMP類型碼
BYTE?code;?????????????????????????//子類型碼
USHORT?checksum;???????????????????//效驗和
????USHORT?id;?????????????????????????//ID號
USHORT?seq;????????????????????????//ICMP數據報的序列號
}IcmpHeader;
//ICMP包頭部
#define?ICMP_ECHO?8????????????????????//請求回送
#define?ICMP_ECHO_REPLY?0??????????????//請求回應
#define?ICMP_MIN?8?????????????????????//ICMP包頭長度(最小ICMP包長度)
#define?STATUS_FAILED?0xFFFF???????????//錯誤碼
#define?DEF_PACKET_SIZE?32?????????????//缺省數據塊長度
#define?MAX_PACKET?1024????????????????//最大數據塊長度
#define?MAX_PING_PACKET_SIZE?(MAX_PACKET+sizeof(IpHeader))
//最大接收數據包長度
void?fill_icmp_data(char?*int);
USHORT?checksum(USHORT?*int);
void?decode_resp(char?*intstruct?sockaddr_in?*);
DWORD?WINAPI?FindIP(LPVOID?pIPAddrTemp);
//函數的申明
WSADATA?wsaData;
SOCKET?sockRaw;?????????????//原始套接字
struct?sockaddr_in?destfromend;
//dest:搜索目的IP,
//from:接收ICMP包的源IP
//end:搜索終止IP。
int?fromlen=sizeof(from);??????????????????????//接收ICMP包長度
char?*recvbuf=new?char[MAX_PING_PACKET_SIZE];??//接受ICMP包緩沖區
unsigned?int?addr=0;???????????????????????????//IP地址
long?ThreadNumCounter=0ThreadNumLimit=20;?????//線程數及最大允許線程數
long?*aa=&ThreadNumCounter;
//全局變量的申明
void?main()//(int?argcchar?*argv[])
{
/*if(argc!=3)???????????????????????????????//判斷格式是否正確
{
cout<<“輸入格式錯誤:scanhost?start_ip?end_ip“< return;
}*/
char?ip1[100]ip2[100];
cout<<“Please?input?IP1?IP2“< cin?>>?ip1?>>?ip2;
cout<<“ip1=“< ????cout<<“ip2=“< ????cout?<
if(WSAStartup(MAKEWORD(21)&wsaData)!=0)
{
cout<<“WSAStartup?failed:“< ExitProcess(STATUS_FAILED);
}
//創建原始套接字
sockRaw=WSASocket(AF_INETSOCK_RAWIPPROTO_ICMPNULL0WSA_FLAG_OVERLAPPED);
if?(sockRaw==INVALID_SOCKET)
{
cout<<“WSASocket()?failed:“< ExitProcess(STATUS_FAILED);
}
int?timeout=1000;
int?bread=setsockopt(sockRawSOL_SOCKETSO_RCVTIMEO(char*)&timeoutsizeof(timeout));
if(bread==SOCKET_ERROR)
{
cout<<“failed?to?set?recv?timeou:“< ExitProcess(STATUS_FAILED);
}
tim
- 上一篇:Disruptor C++版(僅支持單生產者)
- 下一篇:c語言流程圖生成器
評論
共有 條評論