Linux TCP 通信并发

Linux TCP 通信并发_第1张图片

多进程

客户端

#include 
#include 
#include 
#include 
#include 
int main() {
    //创建套接字
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    if(lfd == -1) {
        perror("socket");
        exit(-1);
    }
    //连接服务器
    struct sockaddr_in serveraddr;
    inet_pton(AF_INET, "192.168.245.129", &serveraddr.sin_addr.s_addr);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(9999);
    int ret = connect(lfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
    if(ret == -1) {
        perror("connect");
        _exit(-1);
    }
    //通信
    int i = 0;
    char receiveBuf[1024] = {0};
    while(1) {
        sprintf(receiveBuf, "data: %d\n", i++);

        write(lfd, receiveBuf, strlen(receiveBuf));
        sleep(1);
        
        int len = read(lfd, receiveBuf, sizeof(receiveBuf));
        if(len == -1) {
            perror("read");
            exit(0);
        } else if(len > 0) {
            printf("receive server data: %s\n", receiveBuf);
        } else if (len == 0) {
            printf("server closed...");
        }
        
    }
    

    close(lfd);
}

服务器端

#define _XOPEN_SOURCE
#include
#include
#include
#include
#include
#include
#include
#include

void recyleChild(int arg) {
    while(1) {
        int ret = waitpid(-1, NULL, WNOHANG);
        if(ret == -1) {
            break;
        } else if (ret == 0) {
            break;
        } else if (ret > 0) {
            printf("pid %d is resaved\n", ret);
        }
    }
}

int main() {

    //注册信号捕捉函数
    struct sigaction act;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    act.sa_handler = recyleChild;
    sigaction(SIGCHLD, &act, NULL);


    //创建socket
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    if(lfd == -1) {
        perror("socket");
        exit(0);
    }
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    saddr.sin_addr.s_addr = INADDR_ANY;

    //绑定
    int ret = bind(lfd, (struct sockaddr*)&saddr, sizeof(saddr));
    if(ret == -1) { 
        perror("bind");
        exit(0);
    }

    //监听
    ret = listen(lfd, 128);
    if(ret == -1) { 
        perror("listen");
        exit(0);
    }

    //接受链接
    while(1) {
        struct sockaddr_in cliaddr;
        int len = sizeof(cliaddr);
        
        int cfd = accept(lfd, (struct sockaddr*)&cliaddr, &len);   
        if(cfd == -1) {
            if(errno == EINTR) {
                continue;
            }
            perror("accept");
            exit(-1);
        }

        //每连接一个,创建一个子进程
        pid_t pid = fork();
        if(pid == 0) {
            char clientIP[16];
            inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, clientIP, 16);
            unsigned short cliPort = ntohs(cliaddr.sin_port);
            printf("Client Port is %d, client addr is %s\n", cliPort, clientIP);

            char recvBuf[1024] = {0};
            while(1) {
                int len = read(cfd, &recvBuf, sizeof(recvBuf));
                if(len == -1) {
                    perror("read");
                    return 0;
                } else if(len > 0) {
                    printf("recv client data : %s \n", recvBuf);
                    
                } else {
                    printf("closed.....");
                }

                write(cfd, &recvBuf, sizeof(recvBuf));
            }
            close(cfd);
            exit(0);
        }
    }
    close(lfd);
    return 0;
}

多线程

服务器

#define _XOPEN_SOURCE
#include
#include
#include
#include
#include
#include
#include


struct sockinfo {
    int fd;
    struct sockaddr_in saddr;
    pthread_t tid;
};

struct sockinfo sockinfos[128];

void* working(void* arg) {

    struct sockinfo* pinfo = (struct sockinfo*) arg;
    char clientIP[16];
    inet_ntop(AF_INET, &pinfo->saddr.sin_addr.s_addr, clientIP, 16);
    unsigned short cliPort = ntohs(pinfo->saddr.sin_port);
    printf("Client Port is %d, client addr is %s\n", cliPort, clientIP);

    char recvBuf[1024] = {0};
    while(1) {
        int len = read(pinfo->fd, &recvBuf, sizeof(recvBuf));
        if(len == -1) {
            perror("read");
            return 0;
        } else if(len > 0) {
            printf("recv client data : %s \n", recvBuf);
                    
        } else {
            printf("closed.....");
        }

        write(pinfo->fd, &recvBuf, sizeof(recvBuf));
    }
    close(pinfo->fd);
    exit(0);
    return NULL;
}

int main() {
 //创建socket
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    if(lfd == -1) {
        perror("socket");
        exit(0);
    }
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    saddr.sin_addr.s_addr = INADDR_ANY;

    //绑定
    int ret = bind(lfd, (struct sockaddr*)&saddr, sizeof(saddr));
    if(ret == -1) { 
        perror("bind");
        exit(0);
    }

    //监听
    ret = listen(lfd, 128);
    if(ret == -1) { 
        perror("listen");
        exit(0);
    }

    //初始化数据
    int max = sizeof(sockinfos) / sizeof(sockinfos[0]);
    for(int i = 0; i < max; i++) {
        memset(&sockinfos[i], 0, sizeof(sockinfos[i]));
        sockinfos[i].fd = -1;
        sockinfos[i].tid = -1;
    }

    //接受链接
    while(1) {
        struct sockaddr_in cliaddr;
        int len = sizeof(cliaddr);
        
        int cfd = accept(lfd, (struct sockaddr*)&cliaddr, &len);   
        if(cfd == -1) {
            perror("accept");
            exit(-1);
        }

        struct sockinfo * pinfo;
        for(int i = 0; i < max; i++) {
            if(sockinfos[i].fd == -1) {
                pinfo = &sockinfos[i];
                break;
            }
            if(i == max - 1) {
                sleep(1);
                i--;
            }
        }

        pinfo->fd = cfd;
        memcpy(&pinfo->saddr, &cliaddr, len);


        //每连接一个,创建一个子进程

        pthread_create(&pinfo->tid, NULL, working, pinfo);

        pthread_detach(pinfo->tid);
    }
    close(lfd);
    return 0;
}

客户端

#include 
#include 
#include 
#include 
#include 
int main() {
    //创建套接字
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    if(lfd == -1) {
        perror("socket");
        exit(-1);
    }
    //连接服务器
    struct sockaddr_in serveraddr;
    inet_pton(AF_INET, "192.168.245.129", &serveraddr.sin_addr.s_addr);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(9999);
    int ret = connect(lfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
    if(ret == -1) {
        perror("connect");
        _exit(-1);
    }
    //通信
    int i = 0;
    char receiveBuf[1024] = {0};
    while(1) {
        sprintf(receiveBuf, "data: %d\n", i++);

        write(lfd, receiveBuf, strlen(receiveBuf));
        sleep(1);
        
        int len = read(lfd, receiveBuf, sizeof(receiveBuf));
        if(len == -1) {
            perror("read");
            exit(0);
        } else if(len > 0) {
            printf("receive server data: %s\n", receiveBuf);
        } else if (len == 0) {
            printf("server closed...");
        }
        
    }
    

    close(lfd);
}

你可能感兴趣的:(Linux,网络编程,Linux编程入门,linux,tcp/ip,服务器)