-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.h
More file actions
71 lines (55 loc) · 1.75 KB
/
Client.h
File metadata and controls
71 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include <string>
#include <WinSock2.h>
#define MAX_BUFFER_LEN 8196 // 缓冲区长度1024*8
#define DEFAULT_PORT 12345
#define DEFAULT_IP "127.0.0.1"
#define DEFAULT_THREADS 100
#define DEFAULT_MSG "HELLO!"
using std::string;
class CClient;
// 用于发送数据的线程参数
struct THREADPARAMS_WORKER
{
CClient* pClient; // 类指针
SOCKET sock; // 每个线程使用的socket
int nThreadNo; // 线程编号
char szBuffer[MAX_BUFFER_LEN];
};
// 产生socket连接的线程参数
struct THREADPARAMS_CONNECTION
{
CClient* pClient;
};
class CClient
{
public:
CClient();
~CClient();
public:
bool LoadSocketLib(); // 加载socket库
void UnloadSocketLib() { WSACleanup(); } // 卸载socket库
bool Start(); // 开始测试
void Stop(); // 停止测试
string GetLocalIP(); // 获得本机IP地址
void SetIP(const string& strIP) { m_strServerIP = strIP; } // 设置服务器IP
void SetPort(const int& nPort) { m_nPort = nPort; } // 设置连接服务器端口
void SetThreads(const int& n) { m_nThreads = n; } // 设置并发线程数量
void SetMessage(const string& strMessage) { m_strMessage = strMessage; } // 设置发送的消息
private:
bool EstablishConnections(); // 建立连接
bool ConnectToServer(SOCKET* pSocket,string strServer,int nPort); // 向服务器进行连接
static DWORD WINAPI _ConnectionThread(LPVOID lpParam); // 用于建立连接的线程
static DWORD WINAPI _WorkerThread(LPVOID lpParam); // 用于发送信息的线程
void CleanUp(); // 释放资源
private:
string m_strServerIP; // 服务器的IP地址
string m_strLocalIP; // 本机IP地址
string m_strMessage; // 发送的消息
int m_nPort; // 服务器端口
int m_nThreads; // 并发线程数量
HANDLE* m_phWorkerThreads; // 工作线程句柄数组
HANDLE m_hConnectionThread; // 连接线程的句柄
HANDLE m_hShutdownEvent; // 系统停止退出事件
THREADPARAMS_WORKER* m_pParamsWorker; // 线程参数
};