当前位置:网站首页>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;
}

【下一篇】:多线程版本
边栏推荐
- C WPF realizes dynamic loading of controls through binding
- Mathematical knowledge: fast power fast power
- 职场必备的30套报表模板,满足95%的报表需求,一键套用无需代码
- 链游飞船开发 农民世界链游开发 土地链游开发
- Make a record of glib2.14 upgrading glib2.18 and the principle of the steps
- 《一周的朋友》
- 这道字符串反转的题目,你能想到更好的方法吗?
- mysql中多表视图性能疑惑
- Judge black production based on CDN and client slow log characteristics
- 启动appium
猜你喜欢

MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2

Apache Solr 任意文件读取复现

启动appium

How MySQL converts a date to a number

Test APK exception control nettraffic attacker development

3DMAX plug-in development environment configuration and fileexport and utilities template testing

GIF验证码分析

How bootstrap clears floating styles

openni. utils. OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘

C WPF realizes dynamic loading of controls through binding
随机推荐
深度学习------不同方法实现vgg16
《一周的朋友》
左乘右乘矩阵问题
Start appium
[kubernetes] download address of the latest version of each major version of kubernetes
某年某月某公司的面试题(1)
Detailed explanation of redis persistence, master-slave and sentry architecture
HCIP之路
Distributed ID generation
Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
Decoding and practice of cmaf Technology
Analysis of open API design specification
数学知识:快速幂—快速幂
快速删除代码里面的node_modules
279. perfect square
The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse
深度学习------卷积(conv2D)底层
【markdown】markdown 教程大归纳
Live broadcast review | how can the container transformation of traditional applications be fast and stable?
Unity picture loading and saving