美文网首页
c语言socket epoll示例

c语言socket epoll示例

作者: 一路向后 | 来源:发表于2021-08-28 22:10 被阅读0次

1.server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#define MAXLINE 10
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 5555
#define INFTIM 1000

void setnonblocking(int sock)
{
    int opts;

    opts = fcntl(sock, F_GETFL);
    if(opts < 0)
    {
        perror("fcntl(sock, F_GETFL)");
        exit(1);
    }

    opts = opts | O_NONBLOCK;

    if(fcntl(sock, F_SETFL, opts) < 0)
    {
        perror("fcntl(sock, F_SETFL, opts)");
        exit(1);
    }
}

int main()
{
    int listenfd, connfd, sockfd, epfd, nfds, maxi, i;
    ssize_t n;
    char line[MAXLINE];
    socklen_t clilen = sizeof(struct sockaddr);
    struct epoll_event ev, events[20];
    struct sockaddr_in clientaddr;
    struct sockaddr_in serveraddr;

    epfd = epoll_create(256);

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if(listenfd < 0)
    {
        perror("socket create\n");
        exit(-1);
    }

    setnonblocking(listenfd);

    ev.data.fd = listenfd;

    ev.events = EPOLLIN | EPOLLET;

    epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);

    bzero(&serveraddr, sizeof(serveraddr));
    bzero(&clientaddr, sizeof(clientaddr));

    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons(SERV_PORT);

    bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));

    if(listen(listenfd, LISTENQ) < 0)
    {
        perror("listen");
        exit(-1);
    }

    printf("listen successful\n");

    maxi = 0;

    while(1)
    {
        nfds = epoll_wait(epfd, events, 20, 500);

        /*处理发生的所有事件*/
        for(i=0; i<nfds; i++)
        {
            if(events[i].data.fd == listenfd)
            {
                connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clilen);
                if(connfd < 0)
                {
                    perror("accept");
                    exit(1);
                }

                setnonblocking(connfd);

                char *str = inet_ntoa(clientaddr.sin_addr);

                printf("connect from %s\n", str);

                ev.data.fd = connfd;

                ev.events = EPOLLIN | EPOLLET;

                epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev);
            }
            else if(events[i].events & EPOLLIN)
            {
                if((sockfd=events[i].data.fd) < 0)
                {
                    continue;
                }

                if((n=read(sockfd, line, MAXLINE)) < 0)
                {
                    if(errno == ECONNRESET)
                    {
                        close(sockfd);
                        events[i].data.fd = -1;
                    }
                    else
                    {
                        perror("read line error");
                    }
                }
                else if(n == 0)
                {
                    close(sockfd);
                    events[i].data.fd = -1;
                }

                ev.data.fd = sockfd;

                ev.events = EPOLLOUT | EPOLLET;

                epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
            }
            else if(events[i].events & EPOLLOUT)
            {
                sockfd = events[i].data.fd;

                write(sockfd, line, n);

                ev.data.fd = sockfd;

                ev.events = EPOLLIN | EPOLLET;

                epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
            }
        }
    }

    return 0;
}

2.client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <errno.h>

int main()
{
    struct sockaddr_in address;
    char ch = 'A';
    int cli_fd;
    int len;
    int ret;

    cli_fd = socket(AF_INET, SOCK_STREAM, 0);

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(5555);

    len = sizeof(address);

    ret = connect(cli_fd, (struct sockaddr *)&address, len);
    if(ret == -1)
    {
        perror("oops: client 2");
        exit(-1);
    }

    /*第一次读写*/
    write(cli_fd, &ch, 1);

    ch = 0x00;

    read(cli_fd, &ch, 1);

    printf("the first time: char from server = %c\n", ch);

    sleep(5);

    /*第二次读写*/
    ch = 'B';

    write(cli_fd, &ch, 1);

    ch = 0x00;

    read(cli_fd, &ch, 1);

    printf("the second time: char from server = %c\n", ch);

    close(cli_fd);

    return 0;
}

3.编译程序

$ gcc -o server server.c
$ gcc -o client client.c

4.运行及其结果

$ ./server
listen successful
connect from 127.0.0.1
$ ./client
the first time: char from server = A
the second time: char from server = B

相关文章

网友评论

      本文标题:c语言socket epoll示例

      本文链接:https://www.haomeiwen.com/subject/abuiiltx.html