当前位置:网站首页>socket编程(多进程)
socket编程(多进程)
2022-06-23 07:25:00 【编程小段】
利用多进程实现多个客户端和一个服务端的CS模型
【上一篇】:一个客户端和一个服务器模型
利用父子进程实现,父进程监听接受新的连接,子进程处理新的连接。父进程还负责回收子进程
accept和read是阻塞函数,会被信号打断,此时不应该视为一个错误,需要加一个判断:errno=EINTER
服务端代码如下,客户端代码不变
#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 <sys/wait.h>
#include <errno.h>
//信号处理函数
void waitchild()
{
pid_t wpid;
while(1)
{
wpid = waitpid(-1, NULL, WNOHANG);
if(wpid > 0)
{
printf("child exit\n");
}
else if(wpid == 0 || wpid == -1)
{
break;
}
}
}
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);
//阻塞SIGCHLD信号,避免处理函数还未注册就产生信号
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, NULL);
struct sockaddr_in client;
socklen_t len;
int cfd;
char ip[16];
pid_t pid;
while(1)
{
len = sizeof(client);
cfd = accept(lfd, (struct sockaddr*)&client, &len);
if(errno == EINTR)
{
cfd = accept(lfd, (struct sockaddr*)&client, &len);
}
memset(ip, 0, sizeof(ip));
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);
pid = fork();
if(pid < 0)
{
perror("fork error\n");
exit(-1);
}
else if(pid > 0)
{
close(cfd);
//注册回调函数
struct sigaction act;
act.sa_handler = waitchild;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
//解除对SIGCHLD信号的阻塞
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
else
{
close(lfd);
int n = 0;
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;
}
//父进程和创建出来的子进程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("port == [%d]: n == [%d], buf == [%s]\n", ntohs(client.sin_port), n, buf);
for(int i=0; i<n; i++)
{
buf[i] = toupper(buf[i]);
}
write(cfd, buf, n);
}
close(cfd);
//避免通信结束后子进程循环创建子进程
exit(0);
}
}
close(lfd);
return 0;
}

【下一篇】:多线程版本
边栏推荐
- 职场必备的30套报表模板,满足95%的报表需求,一键套用无需代码
- ArcMap批量删除距离较近的点
- [veusz] import 2D data in CSV
- Acwing game 56 [End]
- 通过端口查文件
- [Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?
- Abnormal logic reasoning problem of Huawei software test written test
- Mathematical knowledge: fast power inverse element - fast power
- 3dmax插件开发环境配置及FileExport和Utilities模板测试
- What is customer experience automation?
猜你喜欢

HCIP之路

Deploy kubersphere in kubernetes

To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"

JS to determine the added and decreased elements of two arrays

EXCEL VBA 入门与实用例子
![[* * * array * * *]](/img/fe/2520d85faab7d1fbf5036973ff5965.png)
[* * * array * * *]

Yan's DP analysis

Hcip Road

Make a record of glib2.14 upgrading glib2.18 and the principle of the steps

YGG 西班牙 subDAO——Ola GG 正式成立
随机推荐
左乘右乘矩阵问题
Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
What is the experience of being a data product manager in the financial industry
论文写作之WPS安装Mathtype插件编写数学公式
Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
Minio single node deployment Minio distributed deployment fool deployment process (I)
Test APK exception control nettraffic attacker development
Hcip Road
HCIP之路第八次实验
Gif verification code analysis
Unity picture loading and saving
[markdown] markdown tutorial summary
What is customer experience automation?
279. perfect square
How MySQL converts a date to a number
Eureka service registration and discovery
Pseudocode specification, pseudocode online editor,
Operation on a bit of binary
深度学习------不同方法实现vgg16
The road to hcip MPLS