資源簡介
編寫程序,監控本地網絡,捕獲一段時間內以本機為源地址或目的地址的IP數據包(不包括以廣播形式發出的數據包),統計IP數據包的信息,列出本機與其他主機之間不同協議類型IP數據包的流量。
代碼片段和文件信息
#include?
#include?
#include?
#include
#include??
#pragma?comment(lib“ws2_32“)?//指定連接到網絡應用和internet
#define?IO_RCVALL?_WSAIOW(IOC_VENDOR1)?
typedef?struct?IP_HEAD?
{?
union?//定義聯合
{?
unsigned?char?Version;?
unsigned?char?HeadLen;?
};?
unsigned?char?ServiceType;?
unsigned?short?TotalLen;?
unsigned?short?Identifier;?
union?
{?
unsigned?short?Flags;?
unsigned?short?FragOffset;?
};?
unsigned?char?TimeToLive;?
unsigned?char?Protocol;?
unsigned?short?HeadChecksum;?
unsigned?int?SourceAddr;?
unsigned?int?DestinAddr;?
unsigned?char?Options;?
}ip_head;?//定義IP頭部的數據結構
void?main(int?argcchar?*argv[])?
{?
using?namespace?std;?
ofstream?outfile(“C:\\logfile.txt“ios::out);
/*if(argc!=2)?
{?
cout< return;
}?*/
WSADATA?WSAData;?
if(WSAStartup(MAKEWORD(22)?&WSAData)!=0)?
{?
cout< return;?
}?
SOCKET?sock=socket(AF_INETSOCK_RAWIPPROTO_IP);?//三個參分別為通信發生的區字段,套接字的類型,與IP協議
if(sock==INVALID_SOCKET)?
{?
cout< closesocket(sock);?
WSACleanup();?
}?
BOOL?flag=TRUE;?
if(setsockopt(sockIPPROTO_IPIP_HDRINCL(char?*)?&flagsizeof(flag))==SOCKET_ERROR)?
{?
cout< closesocket(sock);?
WSACleanup();?
}?
char?hostName[128];//獲取主機名?
if(gethostname(hostName100)==SOCKET_ERROR)?
{?
cout< closesocket(sock);?
WSACleanup();?
}?
hostent?*pHostIP;?//獲取本地IP
if((pHostIP=gethostbyname(hostName))==NULL)?
{?
cout< closesocket(sock);?
WSACleanup();?
}?
sockaddr_in?host_addr;//?
host_addr.sin_family=AF_INET;?
host_addr.sin_port=htons(6000);?
host_addr.sin_addr=*(in_addr?*)pHostIP->h_addr_list[0];?
if(bind(sock(PSOCKADDR)&host_addrsizeof(host_addr))==SOCKET_ERROR)?
{?
cout< closesocket(sock);?//綁定網卡
WSACleanup();?
}?
DWORD?dwBufferLen[10];?
DWORD?dwBufferInLen=1;?
DWORD?dwBytesReturned=0;?
if(WSAIoctl(sock??IO_R
- 上一篇:圖像處理連通域算法 c++ vc 6.0
- 下一篇:MFC圖像處理熒光檢測算法代碼
評論
共有 條評論