socket编程 -- epoll模型服务端/客户端通信的实现

https://blog.csdn.net/y396397735/article/details/50680359

本例实现如下功能: 

支持多客户端与一个服务端进行通信,客户端给服务端发送字符串数据,服务端将字符串中小写转为大写后发送回客户端,客户端打印输出经转换后的字符串。 
例如:发送 abcde,打印输出 ABCDE 
服务端源码如下:
/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>

#define BUFSIZE 666
#define SERV_PORT 8000
#define OPEN_MAX 1024

int main()
{
    int i, j, maxi, listenfd, connfd, sockfd;
    int nready, efd, res;
    ssize_t n;
    char buf[BUFSIZE], str[INET_ADDRSTRLEN];
    socklen_t clilen;
    int client[OPEN_MAX];
    struct sockaddr_in cliaddr, servaddr;

    struct epoll_event tep, ep[OPEN_MAX];//监听事件
    /*分配一个网络通信套接字,监听文件描述符listenfd*/
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    /*初始化 IP类型 端口*/
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);
    /*将listenfd绑定服务端地址*/
    bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

    /*监听请求*/
    listen(listenfd, 20);
    /*将客户端标识初始化为-1*/
    for(i = 0; i < OPEN_MAX; i++){
        client[i] = -1;
    }
    maxi = -1;
    /*告诉内核要监听的文件描述符个数 OPEN_MAX = 1024*/
    efd = epoll_create(OPEN_MAX);

    if(efd == -1){
        perror("epoll_create");
    }

    tep.events = EPOLLIN;/*监听文件描述符的可读事件*/
    tep.data.fd = listenfd;/*设置为监听的文件描述符*/
    /*控制epoll监控的文件描述符上的事件*/
    res = epoll_ctl(efd, EPOLL_CTL_ADD/*注册新的fd到efd*/, listenfd, &tep);

    if(res == -1)
        perror("epoll_ctl");

    for(;;){
        /*等待所监控文件描述符上有事件的产生,阻塞监听*/
        nready = epoll_wait(efd, ep, OPEN_MAX, -1);
        if(nready == -1)
            perror("epoll_wait");
        for(i = 0; i < nready; i++){
            if(!(ep[i].events & EPOLLIN))/*若不是EPOLLIN事件,不做往下的处理*/
                continue;
            if(ep[i].data.fd == listenfd){ /*若是EPOLLIN,执行连接,接受请求*/
                clilen = sizeof(cliaddr);
                /*接受请求,分配新文件描述符connfd进行通信*/
                connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
                printf("received from %s at PORT %d\n", (char*)inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));

                /*若将此新客户端添加至客户端集中*/
                for(j = 0; j < OPEN_MAX; j++)
                    if(client[j] < 0){
                        client[j] = connfd;
                        break;
                    }
                if(j == OPEN_MAX)
                    perror("客户端超过限制");

                if(j > maxi)
                    maxi = j;//保证maxi为最大文件描述符

                tep.events = EPOLLIN;
                tep.data.fd = connfd;
                res = epoll_ctl(efd, EPOLL_CTL_ADD/*注册新的connfd到efd*/, connfd, &tep);
                if(res == -1)
                    perror("epoll_ctl");
            }else{/*处理efd中监听的客户端请求*/
                sockfd = ep[i].data.fd;
                n = read(sockfd, buf, BUFSIZE);
                if(n == 0){ /*读取若为空*/
                    for(j = 0; j <= maxi; j++){
                        if(client[j] == sockfd){
                            client[j] = -1;
                            break;
                        }
                    }
                    /*清除对sockfd文件描述符事件的监听*/
                    res = epoll_ctl(efd, EPOLL_CTL_DEL, sockfd, NULL);
                    if(res == -1)
                        perror("epoll_ctl");
                    close(sockfd);
                    printf("client[%d] closed connection\n", j);
                }else{/*非空则处理客户端信息*/
                    for(j = 0; j<n; j++)
                        buf[j] = toupper(buf[j]);
                    /*写入与客户端通信的文件描述符sockfd*/
                    write(sockfd, buf, n);
                }
            }
        }
    }
    /*关闭监听*/
    close(listenfd);
    close(efd);
    return 0;
}

客户端源码如下:

/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>

#define BUFSIZE 666
#define SERV_PORT 8000
#define OPEN_MAX 1024

int main(int argc, char *argv[])
{
    struct sockaddr_in servaddr;
    char buf[BUFSIZE];
    int sockfd, n;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
    servaddr.sin_port = htons(SERV_PORT);

    /*连接服务端*/
    connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

    while(fgets(buf, BUFSIZE, stdin) != NULL){
        /*通过sockfd给服务端发送数据*/
        write(sockfd, buf, strlen(buf));
        n = read(sockfd, buf, BUFSIZE);
        if(n == 0)
            printf("the other side has been closed.\n");
        else/*打印输出服务端传过来的数据*/
            write(STDOUT_FILENO, buf, n);
    }

    close(sockfd);
    return 0;
}

编译及执行

在终端1先执行:

yu@ubuntu:~/Linux/217/epoll$ ls
client.c  server.c
yu@ubuntu:~/Linux/217/epoll$ gcc -o client client.c
yu@ubuntu:~/Linux/217/epoll$ gcc -o server server.c
yu@ubuntu:~/Linux/217/epoll$ ./server
  • 1
  • 2
  • 3
  • 4
  • 5

另开一终端2:

yu@ubuntu:~/Linux/217/epoll$ ./client
hey how are you
HEY HOW ARE YOU
bye
BYE

再另开一终端3:

yu@ubuntu:~/Linux/217/epoll$ ./client
i am here 007
I AM HERE 007
hehe
HEHE

最后开一终端4:

yu@ubuntu:~/Linux/217/epoll$ ./client
i am 008
I AM 008
zai jian
ZAI JIAN

关闭3个客户端(CTR+C)后服务端窗口:

yu@ubuntu:~/Linux/217/epoll$ ./server
received from 127.0.0.1 at PORT 58385
received from 127.0.0.1 at PORT 58386
received from 127.0.0.1 at PORT 58387
client[2] closed connection
client[1] closed connection
client[0] closed connection

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值