基于多进程的I/O分割

可以将socket的读写操作放到不同的进程,真正实现I/O。

运行效果如图:

server.cpp

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#include <wait.h>

#define MAX_BUFSIZE 1024

void handlError(char* msg) {
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}

void procChild(int sig) {
    int status;
    printf("procChild pid\n");
    pid_t pid = waitpid(-1, &status, WNOHANG);
    if(WIFEXITED(status)) {
        printf("remove pid :%d\n", pid);
        printf("Child send :%d\n\n", WEXITSTATUS(status));
   }
}

// 运行命令格式: ./server 9190
int main(int argc, char **argv) {
    if(argc < 2) handlError("<PORT>");

    int sock = socket(PF_INET, SOCK_STREAM, 0); // SOCK_STREAM, tcp;SOCK_DGRAM, udp
    if(-1 == sock) handlError("socket() error");

    struct sockaddr_in sockAdr;
    memset(&sockAdr, 0, sizeof(sockAdr));
    sockAdr.sin_family = AF_INET;
    sockAdr.sin_port = htons(atoi(argv[1]));
    sockAdr.sin_addr.s_addr = INADDR_ANY;

    if(bind(sock, (struct sockaddr *)&sockAdr, sizeof(sockAdr))) {
        handlError("bind() error");
    }

    struct sigaction action;
    action.sa_handler = procChild;
    action.sa_flags = 0;
    sigemptyset(&action.sa_mask);
    //sigaction(SIGINT, &action, 0);
    int state = sigaction(SIGCHLD, &action, 0);
    printf("state :%d", state);

    if(-1 == listen(sock, 5)) {
        handlError("listen() error");
    }

    int count = 0;
    socklen_t sockAdrLen = sizeof(sockAdr);
    while(true) {
        int clntSock = accept(sock, (struct sockaddr *)&sockAdr, &sockAdrLen);
        if(-1 == clntSock) handlError("accept() error");
        
        count++;

        pid_t pid = fork();
        if(0 == pid) {
            char msg[2] = {0};
            msg[0] = '0' + count;
            write(clntSock, msg, sizeof(msg));
            printf("new pid\n", pid);
            close(clntSock);
            break;
        } else {
            sleep(10);
        }

        close(clntSock);
    }

    close(sock);

    printf("quit child\n");
    return 0;
}

cilent.cpp

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>

#define MAX_BUFSIZE 1024

void handlError(char* msg) {
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}

// 运行命令格式: ./cilent 127.0.0.1 9193
int main(int agrc, char **argv) {
    if(agrc < 3) handlError("<IP> <PORT>");

    int sock = socket(PF_INET, SOCK_STREAM, 0);
    if(-1 == sock) handlError("socket() error");

    struct sockaddr_in sockAdr;
    memset(&sockAdr, 0, sizeof(sockAdr));
    sockAdr.sin_family = AF_INET;
    sockAdr.sin_port = htons(atoi(argv[2]));
    inet_aton(argv[1], &sockAdr.sin_addr);

    if(connect(sock, (struct sockaddr *)&sockAdr, sizeof(sockAdr))) {
        handlError("connect() error");
    }

    char msg[MAX_BUFSIZE] = {0};
    while (ssize_t len = read(sock, msg, sizeof(msg)) && -1 != len) {
        printf("recv :%s\n", msg);
        memset(msg, 0, sizeof(msg));
    }

    close(sock);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值