资源简介
计算机网络课程设计,发现网络中的活动主机。无需调用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 << ip1 << “ - ip - “ << ip2 << endl;
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语言流程图生成器
评论
共有 条评论