当前位置:网站首页>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
边栏推荐
- 《一周的朋友》
- Display proportion of sail soft accumulation diagram
- Using the for loop to output an alphabetic triangle
- GIF验证码分析
- Acwing第 56 場周賽【完結】
- Online text filter less than specified length tool
- [Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?
- The road to hcip MPLS
- Guava Cache 使用小结
- Introduction to MySQL system tables
猜你喜欢

INT 104_LEC 06

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

通过端口查文件

Talk about routing design in service governance

Abnormal logic reasoning problem of Huawei software test written test
![[Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?](/img/ee/ce9f55694b28c391eb07cb11298caf.jpg)
[Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?

Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列

Using the for loop to output an alphabetic triangle

转盘式视觉筛选机及其图像识别系统

To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"
随机推荐
Display proportion of sail soft accumulation diagram
AVL树的实现
Friends of the week
google常用语法
Guava Cache 使用小结
Hackers use new PowerShell backdoors in log4j attacks
带你玩tiktok就这么简单
MySQL slow query record
firewalld 配置文件的位置
Distributed ID generation
ArcMap批量删除距离较近的点
How MySQL converts a date to a number
快速删除代码里面的node_modules
《一周的朋友》
MySQL获取系统时间段
Learn to draw Er graph in an article
左乘右乘矩阵问题
openni.utils.OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘
Qt 使用QDomDocument读取xml文件
MySQL慢查询记录