当前位置:网站首页>socket编程(多线程)
socket编程(多线程)
2022-06-23 07:25:00 【编程小段】
利用多线程实现多个客户端和一个服务端的CS模型
【上一篇】:多进程版本
服务端代码如下,客户端代码不变
#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;//若为-1表示可用,大于0表示已被占用
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;
}
//子线程回调函数
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;
}
//赋值
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;
}
【下一篇】:select模型
边栏推荐
- 【唠嗑篇】普通人到底该怎么学技术啊?
- Deploy kubersphere in kubernetes
- 浅谈ThreadLocal和InheritableThreadLocal,源码解析
- [interface automation] software testing the core skills of salary increase to increase salary by 200%
- 1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task
- Make a record of glib2.14 upgrading glib2.18 and the principle of the steps
- 279. perfect square
- How to tag and label naming before the project release
- Mathematical knowledge: fast power inverse element - fast power
- 快速排序 + 冒泡排序 + 插入排序 + 选择排序
猜你喜欢

Playwirght getting started
![[深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图](/img/f3/ff14cb5439a24e26f977e5f0d15785.png)
[深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图

Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?

Matlab random volatility SV, GARCH using MCMC Markov chain Monte Carlo method to analyze exchange rate time series

YGG Spain subdao Ola GG officially established

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

The eighth experiment of hcip Road

Online JSON to CSharp (c) class tool

一篇文章学会er图绘制

AVL树的实现
随机推荐
How to tag and label naming before the project release
NFS 特别注意权限的问题
mysql中多表视图性能疑惑
Guava Cache 使用小结
MySQL gets the system time period
启动appium
Mathematical knowledge: fast power fast power
MySQL系统表介绍
[Laoke] how should ordinary people learn technology?
Unity audio visualization scheme
数学知识:快速幂求逆元—快速幂
职场必备的30套报表模板,满足95%的报表需求,一键套用无需代码
three. Solution to stripe shadow and grid shadow in JS
Matlab random volatility SV, GARCH using MCMC Markov chain Monte Carlo method to analyze exchange rate time series
抓包发现tcp会话中老是出现重复的ack和大量的tcp重传——SACK(Selective Acknowledgment, 选择性确认)技术
【Veusz】导入CSV中的二维数据
[* * * array * * *]
一秒钟查看一次文件,并将文件最后一行内容结果发送至syslog服务器
Acwing game 56 [End]
Hcip Road