欢迎来到入门教程网!

C语言

当前位置:主页 > 软件编程 > C语言 >

c语言多进程tcp服务器示例

来源:本站原创|时间:2020-01-10|栏目:C语言|点击:

server.h

复制代码 代码如下:

#ifndef SERVER_H
#define SERVER_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/epoll.h>
#include <signal.h>
#include <fcntl.h>
#include "bussiness.h"
#define LISTENTQ 5
#define DEF_PORT 6000
#define MAX_EPOLL_SIZE 10
#define DEF_PROCESS_NUM 5
int create_tcpsvr(char *ip, int port);
void fill_sockaddr(struct sockaddr_in *addr,char *ip, int port);
void comm_to_client(int sockfd);
void epoll_business(int epollfd, int listenfd, struct epoll_event ev);
int init_epoll(int listenfd);
void create_process(int sockfd, int i);
#endif

server.c

复制代码 代码如下:

#include "server.h"
/*
 * Create a TCP Server
 * Return socketfd
 */
int create_tcpsvr(char *ip,int port)
{
    int sockfd;
    struct sockaddr_in addr;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == EOF){
        perror("create_tcpsvr(),socket()");
        exit(EXIT_FAILURE);
    }

    fill_sockaddr(&addr,ip,port);

    if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == EOF){
        perror("create_tcpsvr(),bind()");
        exit(EXIT_FAILURE);
    }

    if (listen(sockfd,LISTENTQ) == EOF) {
        perror("create_tcpsvr(),bind()");
        exit(EXIT_FAILURE);
    }
    return sockfd;
}

/**
 * Set TCP server's address
 */
void fill_sockaddr(struct sockaddr_in *addr, char *ip, int port)
{
    assert(addr);

    addr -> sin_family = AF_INET;
    addr -> sin_port = htons(port?port:DEF_PORT);
    if (ip == NULL) {
        addr -> sin_addr.s_addr = htonl(INADDR_ANY);
    } else if((addr -> sin_addr.s_addr = inet_addr(ip)) == EOF) {
       perror("fill_sockaddr(),inet_addr()");
       exit(EXIT_FAILURE);
    }
}
/*
 * Communicate to client
 */
void comm_to_clt(int listenfd)
{   
    printf("TCP SERVER IS WAITING!\n");

    struct epoll_event events[MAX_EPOLL_SIZE];
    int fd_num;

    int epollfd = init_epoll(listenfd);

    while (1) {

        fd_num = epoll_wait(epollfd,events,MAX_EPOLL_SIZE,-1);
        if (fd_num == EOF) {
            perror("comm_to_clt(),epoll_wait()");
            continue;
        }
        while (--fd_num >= 0) {
            epoll_business(epollfd, listenfd, events[fd_num]);
        }
    }   
}
/*
 * Initlization epoll
 */
int init_epoll(int listenfd)
{
    struct epoll_event ev;
    int epollfd;

    if((epollfd = epoll_create(MAX_EPOLL_SIZE)) == EOF) {
        perror("init_epoll(),epoll_create()");
        exit(EXIT_FAILURE);
    }

    ev.events = EPOLLIN;
    ev.data.fd = listenfd;
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD,listenfd, &ev)) {
        perror("init_epoll(),epoll_ctl()");
        exit(EXIT_FAILURE);
    }
    return epollfd;
}

/*
 * Respond for io change
 */
void epoll_business(int epollfd, int listenfd, struct epoll_event ev)
{
    struct epoll_event ev_n;
    if (ev.data.fd == listenfd) {
        int clt_fd = accept(listenfd, 0, 0);
        if (clt_fd == EOF) {
            perror("epoll_business(), accept()");
            return;
        }

        fcntl(clt_fd, F_SETFL, O_NONBLOCK);
        ev_n.events = EPOLLIN|EPOLLET;
        ev_n.data.fd = clt_fd;
        if (epoll_ctl(epollfd,EPOLL_CTL_ADD, clt_fd, &ev_n) == EOF) {
            perror("epoll_business(), epoll_ctl()");
            return;
        }
    }else {
        main_service(ev.data.fd);
        if (epoll_ctl(epollfd,EPOLL_CTL_DEL, ev.data.fd, &ev) == EOF) {
            perror("epoll_business(), epoll_ctl()");
        }
        close(ev.data.fd);
    }
}
/*
 * Create some process
 */

void create_process(int sockfd, int i)
{
    /*
     * Avoid zombie process
     */
    signal(SIGCHLD,SIG_IGN);

    while (i--) {
        if (fork() == 0) {
            comm_to_clt(sockfd);
        }
    }
    while(1){
        sleep(100);
    }
}
int main(int argc, char *argv[])
{
    int sockfd = create_tcpsvr(0, 0);
    create_process(sockfd,DEF_PROCESS_NUM);
    exit(EXIT_SUCCESS);
}

bussiness.h

复制代码 代码如下:

#ifndef BUSSINESS_H
#define BUSSINESS_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#define BUFF_SIZE 4096
void main_service(int sockfd);
int write_sock(int sockfd, char *buff, size_t length);
int read_sock(int sockfd, char *buff, size_t llength);
#endif


bussiness.c

复制代码 代码如下:

#include "bussiness.h"

void main_service(int sockfd)
{
    char buff[BUFF_SIZE];
    size_t buff_len;
    if ((buff_len = read_sock(sockfd, buff, BUFF_SIZE)) == EOF) {
        perror("main_service(),read_sock()");
        return;
    } else {
  //业务逻辑从这里开始
        printf("%s", buff);
    }

}
/*
 * Recive messages
 * Return the length
 */
int read_sock(int sockfd, char *buff, size_t length)
{
    assert(buff);
    return read(sockfd, buff, length);
}
/*
 * Send messages
 * Return the length
 */
int write_sock(int sockfd, char *buff,size_t length)
{
    assert(buff);
    return write(sockfd, buff, length);
}

上一篇:堆基本操作实现最大堆

栏    目:C语言

下一篇:c++运算符重载基础知识详解

本文标题:c语言多进程tcp服务器示例

本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3749.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有