1 Star 0 Fork 24

wendy.liu/socket

forked from eclipser/socket 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
client2.cpp 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2014-02-27 22:38 . 编写一个简单的client
/*
* File: client2.cpp
* Author: Vicky.H
* Email: eclipser@163.com
*/
#include "Socket.h"
#include <iostream>
#include <thread>
#include <atomic>
#include <string.h>
/*
*
*/
int main(void) {
Socket client("192.168.1.98", 8080);
std::atomic<bool> flag(true);
if (client.connect()) {
// READ
std::thread t1(
[&client, &flag]() {
std::cout << "READ THREAD IS RUNING" << std::endl;
char buf[256] = {0};
while(flag.load()) {
int n = client.receive(buf, 256);
std::cout << "recv:" << buf << std::flush;
memset(buf, 0, n);
}
std::cout << "READ THREAD IS OVER" << std::endl;
}
);
t1.detach();
std::this_thread::sleep_for(std::chrono::microseconds(100));
// WRITE
std::thread t2(
[&client, &flag](){
std::cout << "WRITE THREAD IS RUNING" << std::endl;
char buf[256] = {0};
while(flag.load()) {
std::cin >> buf;
int n = client.send(buf, strlen(buf));
if (strncmp("exit", buf, 4) == 0) {
flag.exchange(false);
}
memset(buf, 0, n);
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
client.close();
std::cout << "READ THREAD IS OVER" << std::endl;
}
);
t2.join();
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wendy.liu/socket.git
git@gitee.com:wendy.liu/socket.git
wendy.liu
socket
socket
master

搜索帮助