1 Star 0 Fork 24

xul/socket

forked from eclipser/socket 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server2.cpp 2.89 KB
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2014-02-27 20:16 . 编写ServerSocket的测试用例
/*
* File: server2.cpp
* Author: Vicky.H
* Email: eclipser@163.com
*/
#include "ServerSocket.h"
#include <iostream>
#include <thread>
#include <atomic>
#include <vector>
#include <string.h>
#include <algorithm>
/*
*
*/
int main(void) {
ServerSocket server(8080);
if (server.isNonBlocking()) {
server.setNonBlocking(true);
std::cout << "set nonblocking !" << std::endl;
}
std::vector<Socket*> clients;
std::atomic<bool> flag1(true);
std::thread t1(
[&server, &clients, &flag1]() {
std::cout << "Thread1 is running" << std::endl;
while (flag1.load()) {
Socket* _client = new Socket();
if (server.accept(_client)) {
clients.push_back(_client);
std::cout << "New Client" << _client->m_Host << ":" << _client->m_Port << std::endl;
}
}
std::cout << "Thread1 Over" << std::endl;
}
);
t1.detach();
std::thread t2(
[&clients, &flag1]() {
std::cout << "Thread2 is running" << std::endl;
while (flag1.load()) {
;
clients.end();
for (Socket* _client : clients) {
if (_client) {
char buf[256];
int recv = _client->receive(buf, 256);
std::cout << _client->m_Host << ":" << _client->m_Port << " " << time(0) << " :\n" << buf << "recv:" << recv << std::endl;
// if (strncmp("exit", "buf", 4) == 0) {
// }
}
}
}
std::cout << "Thread2 Over" << std::endl;
}
);
t2.detach();
// 系统公告
std::thread t3(
[&clients, &flag1]() {
std::cout << "Thread3 is running" << std::endl;
while (flag1.load()) {
std::this_thread::sleep_for(std::chrono::seconds(20));
char buf[256] = {0};
int n = sprintf(buf, "NOW:%d Online:[%d]\n", time(0), clients.size());
for (Socket* _client : clients) {
if (_client) {
_client->send(buf, n);
}
}
}
std::cout << "Thread3 Over" << std::endl;
}
);
std::this_thread::sleep_for(std::chrono::seconds(1));
char buf[64] = {0};
for (;;) {
std::cout << "<<" << std::flush;
std::cin >> buf;
if (strncmp("exit", buf, 4) == 0) {
flag1.exchange(false);
server.close();
std::cout << "Over" << std::endl;
break;
}
memset(buf, 0, 64);
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xuliang5/socket.git
git@gitee.com:xuliang5/socket.git
xuliang5
socket
socket
master

搜索帮助