📄 multhreadsever.cpp
字号:
// MulThreadSever.cpp
#include "stdafx.h"
#include "MulThreadSever.h"
//#include "ActivClient.h"
#include "DataToDB.h"
using namespace std;
#pragma comment(lib,"wsock32.lib")
#define BUFF_SIZE 10*1024
#define ServerPort 5530
//用来接收客户端数据的数据结构
typedef struct rec_data
{
char buff[BUFF_SIZE];
int len;
} data;
//传给子线程的参数的数据结构
typedef struct CLIENT_THREAD_PARAMETER
{
SOCKET socketClient;
char ClientIP[16];
}client_thread_parameter;
//全局变量声明
SOCKET listenSocket;//侦听套接字
//static char szWebRoot[_Max_PATN];//web跟目录
HANDLE ghExit;//结束事件句柄
HANDLE gdwListenThread;//侦听线程句柄
DWORD gdwClientCount=0;//客户连接计数
CRITICAL_SECTION gcriticalClients;//临界区对象
HANDLE ghNoClients;//客户事件句柄
unsigned long _stdcall ClientThread(void *pVoid);
void DeleteClientCount(void);
void IncrementClientCount(void);
void DecrementClientCount(void);
HANDLE InitClientCount(void); //初始化客户连接
//===============================================================
// 函 数 名:CPU_ativity
// 功能描述:提取CPU事件的各个字段,并写入数据库
// 输入参数:string data,char client_IP[]
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void CPU_ativity(string data,char client_IP[])
{
char client[16]={0},time[9]={0};
strcpy(client,client_IP);//client 获取
int usage;
string temp;
int l_firstpos=0,l_secondpos=0;
get_next_position(data,&l_firstpos,&l_secondpos);//移到第1个字段
strcpy(time,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//time 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第2个字段
get_next_position(data,&l_firstpos,&l_secondpos);//移到第3个字段
usage=atoi(temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//usage 提取
// * * * * * * * * 陈欢补充代码* * * * * * * * * * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DataToDB::AddCpuData( client, time, usage );
UpdateCpuUsage( client, usage ); // cpu 0
}
//===============================================================
// 函 数 名:NET_ativity
// 功能描述:提取NET事件的各个字段,并写入数据库
// 输入参数:string data,char client_IP[]
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void NET_ativity(string data,char client_IP[])
{
char client[16]={0},time[9]={0},direction[4]={0},protocol[8]={0},address[9]={0};
int repeat,tcpflag,port,size,l_firstpos=0,l_secondpos=0;
string temp;
strcpy(client,client_IP);//client 获取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第1个字段
strcpy(time,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//time 提取
get_next_position(data,&l_firstpos,&l_secondpos);
get_next_position(data,&l_firstpos,&l_secondpos);//移到第1个双引号字段
repeat=atoi(temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//repeat 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第2个双引号字段
strcpy(direction,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//direction 提取;
get_next_position(data,&l_firstpos,&l_secondpos);//移到第5个字段
strcpy(protocol,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//protocol 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第6个字段
tcpflag=atoi(temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//tcpflag 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第7个字段
strcpy(address,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//address 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第8个字段
port=atoi(temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//port 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第9个字段
size=atoi(temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//size 提取
// * * * * * * * * 陈欢补充代码* * * * * * * * * * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DataToDB::AddNetData( client, time, repeat, direction,
protocol, tcpflag, address, port, size );
IncreaseDataSum ( client, 3 );
}
//===============================================================
// 函 数 名:FILE_ativity
// 功能描述:提取FILE事件的各个字段,并写入数据库
// 输入参数:string data,char client_IP[]
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void FILE_ativity(string data,char client_IP[])
{
char client[16]={0},time[9]={0},path[255]={0},action[7]={0};
string temp;
int l_firstpos=0,l_secondpos=0;
strcpy(client,client_IP);//client 获取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第1个字段
strcpy(time,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//time 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第2个字段
get_next_position(data,&l_firstpos,&l_secondpos);//移到第3个字段
strcpy(action,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//action 提取
l_firstpos=data.find_first_of("\"",l_secondpos+1);
l_secondpos=data.find_first_of("\"",l_firstpos+1);//移到第1个双引号字段
strcpy(path,temp.assign(data,l_firstpos+1,l_secondpos-l_firstpos-1).c_str());//path 提取
// * * * * * * * * 陈欢补充代码* * * * * * * * * * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DataToDB::AddFileData( client, time, action, path );
IncreaseDataSum ( client, 1 );
}
//===============================================================
// 函 数 名:PROC_ativity
// 功能描述:提取PROC事件的各个字段,并写入数据库
// 输入参数:string data,char client_IP[]
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void PROC_ativity(string data,char client_IP[])
{
char client[16]={0},time[9]={0},path[255]={0},action[7]={0};
string temp;
int l_firstpos=0,l_secondpos=0;
strcpy(client,client_IP);//client 获取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第1个字段
strcpy(time,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//time 提取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第2个字段
get_next_position(data,&l_firstpos,&l_secondpos);//移到第3个字段
strcpy(action,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//action 提取
l_firstpos=data.find_first_of("\"",l_secondpos+1);
l_secondpos=data.find_first_of("\"",l_firstpos+1);//移到第1个双引号字段
strcpy(path,temp.assign(data,l_firstpos+1,l_secondpos-l_firstpos-1).c_str());//path 提取
// * * * * * * * * 陈欢补充代码* * * * * * * * * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DataToDB::AddNProcData( client, time, action, path );
IncreaseDataSum ( client, 4 );
}
//===============================================================
// 函 数 名:MAIL_ativity
// 功能描述:提取MAIL事件的各个字段,并写入数据库
// 输入参数:string data,char client_IP[]
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void MAIL_ativity(string data,char client_IP[])
{
char client[16]={0},time[9]={0},subject[50]={0},sender[50]={0},recipient[255]={0},attach[255]={0};
string temp;
int l_firstpos=0,l_secondpos=0;
strcpy(client,client_IP);//client 获取
get_next_position(data,&l_firstpos,&l_secondpos);//移到第1个字段
strcpy(time,temp.assign(data,l_firstpos,l_secondpos-l_firstpos).c_str());//time 提取
l_firstpos=data.find_first_of("\"",l_secondpos+1);
l_secondpos=data.find_first_of("\"",l_firstpos+1);//移到第1个双引号字段
strcpy(subject,temp.assign(data,l_firstpos+1,l_secondpos-l_firstpos-1).c_str());//subject 提取
l_firstpos=data.find_first_of("\"",l_secondpos+1);
l_secondpos=data.find_first_of("\"",l_firstpos+1);//移到第2个双引号字段
strcpy(sender,temp.assign(data,l_firstpos+1,l_secondpos-l_firstpos-1).c_str());//sender 提取
l_firstpos=data.find_first_of("\"",l_secondpos+1);
l_secondpos=data.find_first_of("\"",l_firstpos+1);//移到第3个双引号字段
strcpy(recipient,temp.assign(data,l_firstpos+1,l_secondpos-l_firstpos-1).c_str());//recipient 提取
l_firstpos=data.find_first_of("\"",l_secondpos+1);
l_secondpos=data.find_first_of("\"",l_firstpos+1);//移到第4个双引号字段
strcpy(attach,temp.assign(data,l_firstpos+1,l_secondpos-l_firstpos-1).c_str());//attach 提取
// * * * * * * * * 陈欢补充代码* * * * * * * * * * * * * * * * * * * * * * * *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
DataToDB::AddMailData( client, time, subject, sender, recipient, attach );
IncreaseDataSum ( client, 2 );
}
//===============================================================
// 函 数 名:StartSock
// 功能描述:加载SOCKET
// 输入参数:void
// 输出参数:DWORD
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
DWORD StartSock() //用来加载socket
{
WSADATA WSAdata;
// AfxMessageBox("加载socket......\n");
if(WSAStartup(MAKEWORD(2,2),&WSAdata)!=0)
{
// AfxMessageBox("socket加载失败!\n\n");
return(-1);
}
// AfxMessageBox("socket加载成功.\n\n");
return 1;
}
//===============================================================
// 函 数 名:StopAllConnect
// 功能描述:断开与所有客户的连接
// 输入参数:void
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void StopAllConnect()
{
int nRet;
SetEvent( ghExit );//设置退出事件为有信号
CloseHandle( (HANDLE)gdwListenThread );
WaitForSingleObject( (HANDLE)gdwListenThread, 10000000);
nRet = closesocket( listenSocket );
CloseHandle( ghExit );
}
//===============================================================
// 函 数 名:InitClientCount
// 功能描述:初始化客户连接
// 输入参数:void
// 输出参数:HANDLE
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
HANDLE InitClientCount(void) //初始化客户连接
{
gdwClientCount=0;
InitializeCriticalSection(&gcriticalClients);//初始化临界区对象
ghNoClients=CreateEvent(NULL,TRUE,TRUE,NULL);//创建一个事件,人工重置方式,初始为有信号状态
return ghNoClients;
}
//===============================================================
// 函 数 名:ativity_dispatch
// 功能描述:识别出事件记录的类型,然后调用相应的处理函数
// 输入参数:char data[],char IP[],int first,int last
// 输出参数:void
// 创建日期:04-7-16
// 修改日期:
// 作 者:郑富强
// 附加说明:
//===============================================================
void ativity_dispatch(char data[],char IP[],int first,int last)
{
int l_firstpos=0,l_secondpos=0;
string temp="",temp2="";
temp.append(data,first,last-first+1);
get_next_position(temp,&l_firstpos,&l_secondpos);//移到第1个字段
get_next_position(temp,&l_firstpos,&l_secondpos);//移到第2个字段
temp2.assign(temp,l_firstpos,l_secondpos-l_firstpos);
if(temp2=="CPU")
CPU_ativity(temp,IP);
else if(temp2== "NET")
NET_ativity(temp,IP);
else if(temp2=="FILE")
FILE_ativity(temp,IP);
else if(temp2=="MAIL")
MAIL_ativity(temp,IP);
else if(temp2=="PROC")
PROC_ativity(temp,IP);
}
//===============================================================
// 函 数 名:ativity_dispatch
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -