当前位置:网站首页>Socket programming (multi process)
Socket programming (multi process)
2022-06-23 07:53:00 【Programming segment】
Using multiple processes to achieve multiple clients and a server CS Model
【 Last one 】: A client and a server model
Using parent-child processes , The parent process listens to accept new connections , Child processes handle new connections . The parent process is also responsible for recycling the child process
accept and read Blocking function , Will be interrupted by the signal , This should not be considered an error , Need to add a judgment :errno=EINTER
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 <sys/wait.h>
#include <errno.h>
// Signal processing functions
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);
// Blocking SIGCHLD The signal , Avoid generating signals before the handler is registered
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);
// Register callback function
struct sigaction act;
act.sa_handler = waitchild;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
// Release right SIGCHLD Signal blocking
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;
}
// Parent process and created child process client They don't affect each other ( Read share write copy )
//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);
// Avoid creating child processes after the communication is over
exit(0);
}
}
close(lfd);
return 0;
}

【 Next 】: Multithreaded version
边栏推荐
- Introduction to Excel VBA and practical examples
- 深度学习------不同方法实现lenet-5模型
- Online text filter less than specified length tool
- Introduction to MySQL system tables
- Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
- QT project error: -1: error: cannot run compiler 'clang++' Output:mingw32-make. exe
- unity 音频可视化方案
- 深度学习------不同方法实现vgg16
- Unity图片加载和保存
- How to tag and label naming before the project release
猜你喜欢

INT 104_LEC 06

Vs problems when connecting to SQL myconn OPen(); cannot execute

QT project error: -1: error: cannot run compiler 'clang++' Output:mingw32-make. exe

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

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

socket编程(多进程)

Deploy kubersphere in kubernetes

How to tag and label naming before the project release

忽略超长参数违规

在kubernetes中部署kubersphere
随机推荐
Eureka service registration and discovery
【markdown】markdown 教程大归纳
google常用语法
Test APK exception control nettraffic attacker development
YGG 西班牙 subDAO——Ola GG 正式成立
快速排序 + 冒泡排序 + 插入排序 + 选择排序
1.概率论-组合分析
[kubernetes] download address of the latest version of each major version of kubernetes
AVL树的实现
On ThreadLocal and inheritablethreadlocal, source code analysis
Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
MySQL系统表介绍
通过端口查文件
数学知识:快速幂—快速幂
这道字符串反转的题目,你能想到更好的方法吗?
@Controller和@RestController的区别?
在kubernetes中部署kubersphere
[veusz] import 2D data in CSV
The eighth experiment of hcip Road
Microsoft Exchange – prevent network attacks