#include"EasyTcpClient.hpp"
#include"CELLTimestamp.hpp"
#include<thread>
#include<atomic>
bool g_bRun = true;
void cmdThread()
{
while (true)
{
char cmdBuf[256] = {};
scanf("%s", cmdBuf);
if (0 == strcmp(cmdBuf, "exit"))
{
g_bRun = false;
printf("退出cmdThread线程\n");
break;
}
else {
printf("不支持的命令。\n");
}
}
}
//客户端数量
const int cCount = 1000;
//发送线程数量
const int tCount = 4;
//客户端数组
EasyTcpClient* client[cCount];
std::atomic_int sendCount = 0;
std::atomic_int readyCount = 0;
void recvThread(int begin, int end)
{
while (g_bRun)
{
for (int n = begin; n < end; n++)
{
client[n]->OnRun();
}
}
}
void sendThread(int id)
{
printf("thread<%d>,start\n", id);
//4个线程 ID 1~4
int c = cCount / tCount;
int begin = (id - 1)*c;
int end = id*c;
for (int n = begin; n < end; n++)
{
client[n] = new EasyTcpClient();
}
for (int n = begin; n < end; n++)
{
//win10 "192.168.1.110" i5 6300
//win7 "192.168.1.114" i7 2670qm
//127.0.0.1
//39.108.13.69
client[n]->Connect("192.168.1.102", 4567);
}
printf("thread<%d>,Connect<begin=%d, end=%d>\n", id, begin, end);
readyCount++;
while (readyCount < tCount)
{//等待其它线程准备好发送数据
std::chrono::milliseconds t(10);
std::this_thread::sleep_for(t);
}
//
std::thread t1(recvThread, begin, end);
t1.detach();
//
Login login[10];
for (int n = 0; n < 10; n++)
{
strcpy(login[n].userName, "lyd");
strcpy(login[n].PassWord, "lydmm");
}
const int nLen = sizeof(login);
while (g_bRun)
{
for (int n = begin; n < end; n++)
{
if (SOCKET_ERROR != client[n]->SendData(login, nLen))
{
sendCount++;
}
}
//std::chrono::milliseconds t(10);
//std::this_thread::sleep_for(t);
}
for (int n = begin; n < end; n++)
{
client[n]->Close();
delete client[n];
}
printf("thread<%d>,exit\n", id);
}
int main()
{
//启动UI线程
std::thread t1(cmdThread);
t1.detach();
//启动发送线程
for (int n = 0; n < tCount; n++)
{
std::thread t1(sendThread,n+1);
t1.detach();
}
CELLTimestamp tTime;
while (g_bRun)
{
auto t = tTime.getElapsedSecond();
if (t >= 1.0)
{
printf("thread<%d>,clients<%d>,time<%lf>,send<%d>\n",tCount, cCount,t,(int)(sendCount/ t));
sendCount = 0;
tTime.update();
}
Sleep(1);
}
printf("已退出。\n");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
EasyTcp调整代码结构.zip
共17个文件
hpp:11个
filters:2个
cpp:2个
需积分: 5 0 下载量 129 浏览量
2021-09-22
09:55:15
上传
评论
收藏 19KB ZIP 举报
温馨提示
EasyTcp调整代码结构
资源详情
资源评论
资源推荐
收起资源包目录
EasyTcp调整代码结构.zip (17个子文件)
EasyTcpClient
CELLTimestamp.hpp 1KB
EasyTcpClient.vcxproj 8KB
EasyTcpClient.hpp 5KB
MessageHeader.hpp 1KB
EasyTcpClient.vcxproj.filters 1KB
client.cpp 2KB
EasyTcpServer
EasyTcpServer.hpp 6KB
CELLClient.hpp 2KB
server.cpp 2KB
CELL.hpp 708B
CELLServer.hpp 6KB
EasyTcpServer.vcxproj 8KB
CELLTimestamp.hpp 1KB
CELLTask.hpp 1KB
MessageHeader.hpp 1KB
INetEvent.hpp 565B
EasyTcpServer.vcxproj.filters 2KB
共 17 条
- 1
smallcat0226
- 粉丝: 0
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0