当前位置:网站首页>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模型
边栏推荐
- Product axure9 (English version), prototype design background dynamic secondary menu display content
- 1278_ FreeRTOS_ Understand the delayed task with the prvaddcurrenttasktodelayedlist interface
- 快速排序 + 冒泡排序 + 插入排序 + 選擇排序
- js中的同步和异步
- 深度学习------不同方法实现lenet-5模型
- 《一周的朋友》
- Guava Cache 使用小结
- 30 sets of report templates necessary for the workplace, meeting 95% of the report needs, and no code is required for one click application
- google常用语法
- Acwing第 56 場周賽【完結】
猜你喜欢

HCIP之路MPLS

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

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

C WPF additional attribute implementation interface defines decorator

测试apk-异常管控NetTraffic攻击者开发

What is customer experience automation?

Online text filter less than specified length tool

深度学习------不同方法实现vgg16

链游飞船开发 农民世界链游开发 土地链游开发

QT reading XML files using qdomdocument
随机推荐
带你玩tiktok就这么简单
The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse
在线JSON转CSharp(C#)Class工具
What is customer experience automation?
openni. utils. OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘
分布式ID生成
MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
2.概率论-概率论公理
《一周的朋友》
Guava cache usage summary
1278_ FreeRTOS_ Understand the delayed task with the prvaddcurrenttasktodelayedlist interface
Operation on a bit of binary
Eureka service registration and discovery
Acwing game 56 [End]
Download the OSS file and modify the file name
Unity picture loading and saving
Take you to tiktok. That's it
How to tag and label naming before the project release
[veusz] import 2D data in CSV
[markdown] markdown tutorial summary