当前位置:网站首页>tcpserver开启多线程处理
tcpserver开启多线程处理
2022-06-21 16:19:00 【拉松】
本例子是《C++socket网络编程大全实战http服务器视频课程》的学习笔记。
服务端开辟线程处理对应的客户端消息,用到了c++11中的thread库。
main.cpp
#include <string.h>
#include <stdlib.h>
#ifdef WIN32//Windows环境下编译
#include <Windows.h>
#else//linux环境下编译
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define closesocket close //linux中使用close函数
#endif
#include <thread>
using namespace std;
class TcpThread
{
public:
void Main()
{
char buf[1024] = {
0 };
for (;;)
{
int recvlen = recv(client, buf, sizeof(buf) - 1, 0);
if (recvlen <= 0) break;
buf[recvlen] = '\0';
if (strstr(buf, "quit") != NULL) {
char re[] = "quit success!\n";
send(client, re, strlen(re) + 1, 0);//strlen获取的字符串大小不包括\0
break;
}
send(client, "ok\n", 4, 0);
printf("recv %s\n", buf);
}
closesocket(client);
delete this;
}
int client = 0;
};
#include <stdio.h>
int main(int argc, char *argv[])
{
#ifdef WIN32
WSADATA ws; //初始化动态链接库
WSAStartup(MAKEWORD(2,2), &ws);//主版本为2,副版本为2
#endif
int sock = socket(AF_INET, SOCK_STREAM, 0);//TCP
if (sock == -1) {
printf("create socket failed!\n");
return -1;
}
unsigned short port = 8080;
if (argc > 1) {
port = atoi(argv[1]);//把字符串转换成整型数
}
sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);//注意字节序转换
saddr.sin_addr.s_addr = htonl(0);
if (bind(sock, (sockaddr*)&saddr, sizeof(saddr)) != 0) {
printf("bind port %d failed!\n", port);
return -2;
}
printf("bind port %d success!\n", port);
listen(sock, 10);
for (;;)
{
sockaddr_in caddr;
socklen_t len = sizeof(caddr);
int client = accept(sock, (sockaddr*)&caddr, &len);
if (client <= 0) break;
printf("accept client %d\n", client);
char *ip = inet_ntoa(caddr.sin_addr);
unsigned short cport = ntohs(caddr.sin_port);
printf("client ip is %s,port is %d\n", ip, cport);
TcpThread *th = new TcpThread();
th->client = client;
thread sth(&TcpThread::Main, th);
sth.detach();
}
closesocket(sock);
getchar();
return 0;
}
Makefile
tcpserver:main.cpp
g++ main.cpp -o tcpserver -std=c++11 -lpthread
测试

边栏推荐
- 鱼佬:电信客户流失预测赛方案!
- [Oracle] is there a "time" data type in oracle-- Research on Oracle data types
- What is the difference between IEC62133 and EN62133? Which items are mainly tested?
- Two understandings of Bayes formula
- nest.js实战之模块依赖传递性
- Jetpack compose management status (I)
- 智慧三农数字王宁:适合初学者的前5日交易秘诀
- How to adjust 3DE 3D model view if you can't see it
- 堆栈认知——堆简介
- Lua导出为外部链接库并使用
猜你喜欢

数字经济王宁教你如何正确地选择短期投资

Iso8191 test is mentioned in as 3744.1. Are the two tests the same?

aws elastic beanstalk入门之简介

Why is rediscluster designed with 16384 slots?

潤邁德醫療通過上市聆訊:預計虧損將增加,霍雲飛兄弟持股約33%

Reids面试题集合 数据结构+穿透雪崩+持久化+内存淘汰策略+数据库双写+哨兵

3DE motion contour data modification

vector的模拟实现

Two understandings of Bayes formula

Jetpack compose phase
随机推荐
SCAU Software Engineering Fundamentals
《MATLAB 神经网络43个案例分析》:第27章 LVQ神经网络的预测——人脸朝向识别
Analysis of 43 cases of MATLAB neural network: Chapter 27 prediction of LVQ Neural Network - face orientation recognition
Vit is crazy, 10+ visual transformer model details
经纬度转换为距离
What is the difference between IEC62133 and EN62133? Which items are mainly tested?
How to write technical documents software engineering at Google
Stack cognition -- basic use of reverse IDA tools
我被变相降薪了
多维分析预汇总应该怎样做才管用?
C语言与Lua的交互(实践二)
大型网站技术架构 | 应用服务器安全防御
Stm32f1 and stm32subeide programming example - linear Hall effect sensor driver
MySQL 1055 error -this is incompatible with SQL_ mode=only_ full_ group_ By solution
Encryption crash, is there a future for Web3 games? In depth discussion of 5 papers
Application architecture principles
性能测试---locust的on_start 和 on_stop 方法
Side effects in compose
鱼佬:电信客户流失预测赛方案!
Kubernetes + Yanrong SaaS data service platform, personalized demand support has never been lost