26 Star 45 Fork 24

eclipser/socket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
client.cpp 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
Administrator 提交于 2014-02-26 20:42 . 编写简单的客户端程序
/*
* File: client.cpp
* Author: Vicky.H
* Email: eclipser@163.com
*/
#include "Socket.h"
#include <iostream>
#include <thread>
#include <atomic>
#include <string.h>
void processRead(Socket* sock, std::atomic<bool>* _flag) {
char buf[256] = {0};
while(_flag->load()) {
unsigned int n = sock->receive(buf, 256);
std::cout << "recv:" << buf << std::endl;
if (strncmp(buf, "exit", 4) == 0) {
_flag->exchange(false);
}
memset(buf, 0, n + 1);
}
std::cout << "processClientRead Over" << std::endl;
}
void processWrite(Socket* sock, std::atomic<bool>* _flag) {
char buf[256] = {0};
while(_flag->load()) {
std::cin >> buf;
sock->send(buf, strlen(buf));
if (strncmp(buf, "exit", 4) == 0) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
_flag->exchange(false);
}
}
}
/*
*
*/
int main(void) {
//TCPSERVER:socket() bind() listen() accept() read()/write() close()/shutdown
//TCPCLIENT:socket() connect() write()/read() close()/shutdown
//sockaddr_in.sin_port = htons(port); 转换int类型的端口
//inet_pton(AF_INET, "192.168.1.78", &sockaddr_in.sin_addr); 转换string类型地址
Socket client("192.168.1.98", 8080);
client.connect();
std::atomic<bool>* _flag = new std::atomic<bool>(true);
std::thread read(processRead, &client, _flag);
read.detach();
std::thread write(processWrite, &client, _flag);
write.join();
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/eclipser/socket.git
git@gitee.com:eclipser/socket.git
eclipser
socket
socket
master

搜索帮助