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

【下一篇】:多线程版本
边栏推荐
- 3DMAX plug-in development environment configuration and fileexport and utilities template testing
- Start appium
- 【Kubernetes】Kubernetes各大版本的最新版本下载地址
- 快速排序 + 冒泡排序 + 插入排序 + 選擇排序
- 这道字符串反转的题目,你能想到更好的方法吗?
- 通过端口查文件
- 抓包发现tcp会话中老是出现重复的ack和大量的tcp重传——SACK(Selective Acknowledgment, 选择性确认)技术
- openni. utils. OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘
- [Planet selection] how to efficiently build fine-grained two-way links between roam and thebrain?
- 分布式ID生成
猜你喜欢

Acwing第 56 场周赛【完结】

SQL注入常用到的绕过方式-ctf

Online text filter less than specified length tool

基于51单片机的温度检测监测报警系统设计

Acwing第 56 場周賽【完結】

Detailed explanation of redis persistence, master-slave and sentry architecture

The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse

Online JSON to CSharp (c) class tool

Interview questions of a company in a certain month of a certain year (1)

深度学习------不同方法实现lenet-5模型
随机推荐
在线JSON转CSharp(C#)Class工具
Which company would like to buy serious illness insurance in 2022?
数学知识:快速幂—快速幂
The road to hcip MPLS
抓包发现tcp会话中老是出现重复的ack和大量的tcp重传——SACK(Selective Acknowledgment, 选择性确认)技术
在线文本过滤小于指定长度工具
Unity图片加载和保存
HCIP之路MPLS
unity转微信小程序小游戏
2022山东大学软件学院软件项目管理期末考试(回忆版)
Quick sort + bubble sort + insert sort + select sort
[markdown] markdown tutorial summary
three. Solution to stripe shadow and grid shadow in JS
1. probability theory - combination analysis
php序列化和反序列化-ctf
【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
Basic experiment of data statistics and analysis - basic grammar and operation
帆软堆积图显示占比
【Veusz】导入CSV中的二维数据
在kubernetes中部署kubersphere