socket实现Tcp多进程并发服务器

代码

multi_process_server.c

#include "wrap.h" //wrap.h将socket,bind,listen等函数进行封装,对返回值进行检测,这里省略具体封装函数的实现,只列出wrap.h中对函数的声明
#include <ctype.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define SERV_PORT 6666
//回调函数,用于回收子进程
void catch_child(int signum)
{
    while(waitpid(0, NULL, WNOHANG) > 0);
    return;
}

int main(int argc, char *argv[])
{
    int listen_fd, connect_fd;
    int ret;
    char buf[BUFSIZ], str[INET_ADDRSTRLEN];
    pid_t pid;
    struct sockaddr_in server_addr, client_addr;

    bzero(&server_addr, sizeof(server_addr));

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERV_PORT);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    listen_fd = Socket(AF_INET, SOCK_STREAM, 0);
    Bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    Listen(listen_fd, 128);
    socklen_t client_addr_len = sizeof(client_addr);

    printf("Accept connections ...\n");
    while(1)
    {
        connect_fd = Accept(listen_fd, (struct sockaddr *)&client_addr, &client_addr_len);
        printf("Connect fd: %d\n", connect_fd);

        pid = fork();
        if(pid < 0) perr_exit("fork error");
        else if(pid == 0)
        {
            Close(listen_fd);  //子进程可以关闭监听套接字
            while(1)
            {
                ret = Read(connect_fd, buf, sizeof(buf));
                if(ret == 0)
                {
                    printf("the other side has been closed. \n");
                    Close(connect_fd);
                    break;
                }
                printf("Received from %s, port: %d\n", 
                        inet_ntop(AF_INET, &client_addr.sin_addr, str, client_addr_len),
                        ntohs(client_addr.sin_port)
                        );

                for(int i=0; i<ret; i++) 
                    buf[i] = toupper(buf[i]);

                Write(connect_fd, buf, ret);
                Write(STDOUT_FILENO, buf, ret);  //将结果打印至标准输出
            }
        }
        else  //父进程使用捕捉信号,并调用回调函数 catch_child 回收子进程资源
        {
            close(connect_fd);  //父进程可以关闭连接套接字
            struct sigaction act;
            sigemptyset(&act.sa_mask);
            act.sa_handler = catch_child;
            act.sa_flags = 0;

            ret = sigaction(SIGCHLD, &act, NULL);
            if(ret == -1) perr_exit("sigaction error");

            continue;
        }
    }

    return 0;
}

wrap.h

#ifndef __WRAP_H_
#define __WRAP_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>

void perr_exit(const char *s);
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);
int Bind(int fd, const struct sockaddr *sa, socklen_t salen);
int Connect(int fd, const struct sockaddr *sa, socklen_t salen);
int Listen(int fd, int backlog);
int Socket(int family, int type, int protocol);
ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *ptr, size_t nbytes);
int Close(int fd);
ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n);
static ssize_t my_read(int fd, char *ptr);
ssize_t Readline(int fd, void *vptr, size_t maxlen);

#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值