驱动 epoll实现并发服务器

//服务器
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define ERRLOG(msg)                                        \
    do {                                                   \
        printf("%s %s %d:", __FILE__, __func__, __LINE__); \
        perror(msg);                                       \
        exit(-1);                                          \
    } while (0)
#if 0
使用epoll完成并发服务器模型:
1、创建红黑树
2、将关心的文件描述符加入红黑树上
3、等待发生IO事件的文件描述符
4、判断哪个文件描述符发生了IO事件并且作出对应逻辑处理
#endif
#define N 32
#define PORT 6666   //端口号的网络字节序 1024~49151
#define IP "192.168.250.100" 
int main(int argc, char* argv[])
{

    int epfd = epoll_create(1);
    if (epfd < 0) {
        ERRLOG("epfd err");
    }
 
    int socketfd;
    if (-1 == (socketfd = socket(AF_INET, SOCK_STREAM, 0))) {
        ERRLOG("socket error");
    }

 
    struct epoll_event event;
    event.events = EPOLLIN | EPOLLET;
    event.data.fd = socketfd;
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, socketfd, &event) < 0) {
        ERRLOG("epoll_ctl error");
    }
    // 填充服务器地址
    struct sockaddr_in serveraddr, clientaddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(PORT);
    serveraddr.sin_addr.s_addr = inet_addr(IP);
    socklen_t serveraddr_len = sizeof(serveraddr);
    socklen_t clientaddr_len = sizeof(clientaddr_len);
    // 绑定
    if (bind(socketfd, (struct sockaddr*)&serveraddr, serveraddr_len) < 0) 
	{
        ERRLOG("bind error");
    }
    // 监听
    if (listen(socketfd, 10) < 0) {
        ERRLOG("listen error");
    }
    int acceptfd, ret, i;
    int bytes;
    struct epoll_event events[20];
    char buf[N] = { 0 };
    while (1) {
        ret = epoll_wait(epfd, events, 20, -1);
        if (ret > 0) {
            for (i = 0; i < ret; i++) {
                if (events[i].data.fd == socketfd) {
                    if (events[i].events & EPOLLIN == EPOLLIN) {
                        acceptfd = accept(events[i].data.fd, (struct sockaddr*)&clientaddr, &clientaddr_len);
                        if (acceptfd < 0) {
                            ERRLOG("accept error");
                        }
                        printf("IP:%s 连接成功\n", (char*)inet_ntoa(clientaddr.sin_addr));
                        struct epoll_event event;
                        event.events = EPOLLIN | EPOLLET;
                        event.data.fd = acceptfd;
                        if (epoll_ctl(epfd, EPOLL_CTL_ADD, acceptfd, &event) < 0) {
                            ERRLOG("epoll_ctl err");
                        }
                    }
                } else {
                    if (EPOLLIN & events[i].events == EPOLLIN) {
                        memset(buf, 0, sizeof(buf));
                        if (-1 == (bytes = recv(events[i].data.fd, buf, sizeof(buf), 0))) {
                            ERRLOG("recv error");
                        } else if (bytes == 0) {
                            printf("acceptfd:%d 断开连接...\n", events[i].data.fd);
                            close(events[i].data.fd);
                            epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
                        } else {
                            printf("buf:%s\n", buf);
                        }
                        strcat(buf, "--hqyj");
                        if (-1 == send(events[i].data.fd, buf, N, 0)) {
                            ERRLOG("send error");
                        }
                    }
                }
            }
        } else {
            ERRLOG("epoll_wait error");
        }
    }
    close(socketfd);
    return 0;
}
//客户端
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#define ERRLOG(msg)                                        \
    do {                                                   \
        printf("%s %s %d:", __FILE__, __func__, __LINE__); \
        perror(msg);                                       \
        exit(-1);                                          \
    } while (0)

#define N 32
#define PORT 1234   //端口号的网络字节序 1024~49151
#define IP "192.168.250.100" 
int main(int argc, const char* argv[])
{

    // 1.创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd) {
        ERRLOG("socket error");
    }

    // 2.填充服务器网络信息结构体
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(PORT);
    serveraddr.sin_addr.s_addr = inet_addr(IP);

    socklen_t serveraddr_len = sizeof(serveraddr);

    char buf[32] = { 0 };
    int nbytes = 0;

    // 3.尝试与服务器建立连接
    if (-1 == connect(sockfd, (struct sockaddr*)&serveraddr, serveraddr_len)) {
        ERRLOG("connect error");
    }
    printf("与服务器建立连接成功..\n");
    while (1) {
        memset(buf, 0, sizeof(buf));
        fgets(buf, N, stdin);
        buf[strlen(buf) - 1] = '\0'; //清理结尾的\n

        //发送数据
        if (-1 == send(sockfd, buf, sizeof(buf), 0)) {
            ERRLOG("send error");
        }
        //接收服务器的应答信息
        if (-1 == (nbytes = recv(sockfd, buf, sizeof(buf), 0))) {
            ERRLOG("recv error");
        }
        if (0 == nbytes) {
            break;
        }
        //输出应答信息
        printf("应答为:[%s]\n", buf);
    }
    //关闭套接字
    close(sockfd);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值