当前位置:网站首页>Socket programming (multithreading)
Socket programming (multithreading)
2022-06-23 07:54:00 【Programming segment】
Using multithreading to achieve multiple clients and a server CS Model
【 Last one 】: Multi process version
The server code is as follows , The client code remains unchanged
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<netinet/in.h>
#include<pthread.h>
typedef struct info
{
int cfd;// if -1 Means available , Greater than 0 Indicates occupied
int index;
pthread_t thread;
struct sockaddr_in client;
}INFO;
INFO thInfo[1024];
void init_thInfo()
{
int i = 0;
for(i=0; i<1024; i++)
{
thInfo[i].cfd = -1;
}
}
int findIndex()
{
int i = 0;
for(i=0; i<1024; i++)
{
if(thInfo[i].cfd == -1)
{
break;
}
}
if(i == 1024)
{
return -1;
}
return i;
}
// Child thread callback function
void* thread_work(void* arg)
{
INFO *p = (INFO*)arg;
printf("idx == [%d]\n", p->index);
int n;
int cfd = p->cfd;
char buf[1024];
while(1)
{
memset(buf, 0, sizeof(buf));
n = read(cfd, buf, sizeof(buf));
if(n <= 0)
{
printf("read error or client close\n");
break;
}
printf("port == [%d]: n == [%d], buf == [%s]\n", ntohs((p->client).sin_port), n, buf);
for(int i=0; i<n; i++)
{
buf[i] = toupper(buf[i]);
}
write(cfd, buf, n);
}
close(cfd);
pthread_exit(NULL);
}
int main()
{
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd < 0)
{
perror("socket error");
return -1;
}
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);
int ret = bind(lfd, (struct sockaddr*)&serv, sizeof(serv));
if(ret < 0)
{
perror("bind error");
return -1;
}
listen(lfd, 128);
init_thInfo();
int cfd;
int idx;
char ip[16];
socklen_t len;
struct sockaddr_in client;
pthread_t thread;
while(1)
{
len = sizeof(client);
bzero(&client, sizeof(client));
cfd = accept(lfd, (struct sockaddr*)&client, &len);
idx = findIndex();
if(idx == -1)
{
close(cfd);
continue;
}
// assignment
thInfo[idx].cfd = cfd;
thInfo[idx].index = idx;
memcpy(&thInfo[idx].client, &client, sizeof(client));
printf("client: ip == [%s], port == [%d]\n", inet_ntop(AF_INET, &client.sin_addr.s_addr, ip, sizeof(ip)), ntohs(client.sin_port));
printf("lfd == [%d], cfd == [%d]\n", lfd, cfd);
pthread_create(&thInfo[idx].thread, NULL, thread_work, &thInfo[idx]);
pthread_detach(thInfo[idx].thread);
}
close(lfd);
return 0;
}
【 Next 】:select Model
边栏推荐
- openni.utils.OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘
- How do I install MySQL on my computer?
- 深度学习------卷积(conv2D)底层
- Pseudocode specification, pseudocode online editor,
- Match 56 de la semaine d'acwing [terminé]
- Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
- 记一次高校学生账户的“从无到有”
- Query on the performance of multi table view in MySQL
- 数学知识:快速幂求逆元—快速幂
- How to tag and label naming before the project release
猜你喜欢

openvino系列 19. OpenVINO 与 PaddleOCR 实现视频实时OCR处理

Acwing第 56 場周賽【完結】

记一次高校学生账户的“从无到有”

Design of temperature detection and alarm system based on 51 single chip microcomputer

YGG 西班牙 subDAO——Ola GG 正式成立

Acwing第 56 场周赛【完结】
![Acwing game 56 [End]](/img/f6/cd650331c819a27f17c9ce6cd0c569.png)
Acwing game 56 [End]

The road to hcip MPLS

某年某月某公司的面试题(1)

浅谈ThreadLocal和InheritableThreadLocal,源码解析
随机推荐
Mathematical knowledge: fast power inverse element - fast power
Hackers use new PowerShell backdoors in log4j attacks
Quick sort + bubble sort + insert sort + select sort
Gif verification code analysis
PHP 文件包含 -ctf
Unity audio visualization scheme
For loop of go language foundation
左乘右乘矩阵问题
MFC Radio Button分组
Eureka服务注册与发现
Introduction to MySQL system tables
Unity to wechat applet games
The road to hcip MPLS
[pyqt5 series] modify the counter to realize control
MySQL慢查询记录
浅谈ThreadLocal和InheritableThreadLocal,源码解析
Tri rapide + Tri par bulle + Tri par insertion + Tri par sélection
YGG 西班牙 subDAO——Ola GG 正式成立
数学知识:快速幂—快速幂
1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task