当前位置:网站首页>Multi thread communication between client and server (primary version)
Multi thread communication between client and server (primary version)
2022-06-29 11:07:00 【Vegetable chicken obsessed with c++】
Specific ideas :
Using multithreading , Let the main thread accept new connections , Let the child thread handle communication with the client ; To use multithreading, set the thread to the separation property , Let the thread reclaim resources after exiting .
Server code :
server.c
// Multithreaded versions of highly concurrent servers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#include <pthread.h>
#include "wrap.h"
// Child thread callback function
void *thread_work(void *arg)
{
int cfd = *(int *)arg;
printf("cfd==[%d]\n", cfd);
int i;
int n;
char buf[1024];
while(1)
{
//read data
memset(buf, 0x00, sizeof(buf));
n = Read(cfd, buf, sizeof(buf));
if(n<=0)
{
printf("read error or client closed,n==[%d]\n", n);
break;
}
printf("n==[%d], buf==[%s]\n", n, buf);
for(i=0; i<n; i++)
{
buf[i] = toupper(buf[i]);
}
// Send data to client
Write(cfd, buf, n);
}
// Close communication file descriptor
close(cfd);
pthread_exit(NULL);
}
int main()
{
// establish socket
int lfd = Socket(AF_INET, SOCK_STREAM, 0);
// Set up port multiplexing
int opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
// binding
struct sockaddr_in serv;
bzero(&serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(8888);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
Bind(lfd, (struct sockaddr *)&serv, sizeof(serv));
// Set listening
Listen(lfd, 128);
int cfd;
pthread_t threadID;
while(1)
{
// Accept new connection
cfd = Accept(lfd, NULL, NULL);
// Create child threads
pthread_create(&threadID, NULL, thread_work, &cfd);
// Set the child thread as the separation property
pthread_detach(threadID);
}
// Close listener file descriptor
close(lfd);
return 0;
}Start the server after compiling , Use nc command nc: It can be used as client launch TCP or UDP Connect . Start multiple terminals at the same time ( Simulation client ) To test .
The test results are as follows , Multiple clients make requests to the server , And get a response , Multi thread communication .

边栏推荐
猜你喜欢

Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集

Qt编写物联网管理平台37-逻辑设计

Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?

CS231n-2022 Module1: 神经网络要点概述(2)

极限导论总结

这个mySQL安装的时候怎么搞去了?

Spark - Task 与 Partition 一一对应与参数详解

Here comes the tutorial of datawhale recommendation system!

【各种**问题系列】OLTP和OLAP是啥?

非凸联合创始人李佐凡:将量化作为自己的终身事业
随机推荐
Free books! The best-selling book "Introduction and practice of OpenCV image processing" has been completed
JS post download file
(JS)数组去除重复
【NLP】文本生成专题1:基础知识
np. astype()
Several methods of enterprise competition analysis: SWOT, Porter five forces, pest "suggestions collection"
Cs231n-2022 module1: overview of key points of neural network (2)
Spark - Task 与 Partition 一一对应与参数详解
Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集
(JS)模仿indexOf方法寻找字符串中某个字符的位置
在线SQL转HTMLTable工具
Map merges the same keys and values into a list
【C语言进阶】文件操作(二)
毕业季·进击的技术er - 职场打工人
STM32F1與STM32CubeIDE編程實例-超聲波測距傳感器驅動
常见电机分类和驱动原理动画[通俗易懂]
Numeric Keypad
MySQL get table information
学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条...
工具箱之 IKVM.NET 项目新进展