Linux----网络编程(IO复用之epoll系统调用函数)

服务器端epoll.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//ser_epoll
#define MAXFD 10

void epoll_add(int epfd, int fd)
{
    struct epoll_event ev;

// ev.events = EPOLLIN | EPOLLET;
    ev.events = EPOLLIN;
    ev.data.fd = fd;

   if(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1)
    {
       perror("epoll ctl error");
   }


}

void epoll_del(int epfd, int fd)
{
    if(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) == -1)
    {
        perror("epoll ctl del error");
    }
}

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    assert(sockfd != -1);

    struct sockaddr_in saddr, caddr;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6000);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int res = bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));
    assert(res != -1);

    listen(sockfd, 5);

    int epfd = epoll_create(MAXFD);

    epoll_add(epfd, sockfd);
    struct epoll_event events[MAXFD];

    while( 1 )
    {
        printf("epoll wait\n");
        int n = epoll_wait(epfd, events, MAXFD, 5000);
        if(n == -1)
        {
            printf("epoll_wait error\n");
            continue;
        }
        else if( n == 0 )
        {
            printf("time out\n");
            continue;
        }
        else
        {
            int i = 0;
            for(; i

客户端cli_epoll.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    assert(sockfd != -1);
    struct sockaddr_in saddr;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6000);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int res = connect(sockfd,(struct sockaddr*)&saddr, sizeof(saddr));
    assert(res != -1);

    while(1)
    {
        char buff[128] = {0};
        printf("input:\n");
        fgets(buff,128,stdin);
        if(strncmp(buff,"end",3) == 0)
        {
            break;
            }
        send(sockfd, buff, strlen(buff), 0);
        memset(buff, 0, 128);
        recv(sockfd, buff, 127, 0);
          printf("buff=%s\n",buff);
        }
      close(sockfd);
      exit(0);
    }

运行结果:

Linux----网络编程(IO复用之epoll系统调用函数)_第1张图片

你可能感兴趣的:(Linux)