c_cpp epoll.c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp epoll.c相关的知识,希望对你有一定的参考价值。

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

#define MAX_EVENTS 10
#define MY_PORT		8080
#define MAXBUF		1024

void setNonblocking(int fd)
{
    int flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}     

int main()
{
    struct epoll_event ev, events[MAX_EVENTS];
    int listen_sock, conn_sock, nfds, epollfd, n;
	struct sockaddr_in self;
    struct sockaddr_in local;
    int addrlen = sizeof(local);
	char buffer[MAXBUF];

    /* Set up listening socket, 'listen_sock' (socket(),
       bind(), listen()) */
    if ( (listen_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
	{
		perror("Socket");
		exit(errno);
	}

	bzero(&self, sizeof(self));
	self.sin_family = AF_INET;
	self.sin_port = htons(MY_PORT);
	self.sin_addr.s_addr = INADDR_ANY;

    if ( bind(listen_sock, (struct sockaddr*)&self, sizeof(self)) != 0 )
	{
		perror("socket--bind");
		exit(errno);
	}

	if ( listen(listen_sock, 20) != 0 )
	{
		perror("socket--listen");
		exit(errno);
	}

    epollfd = epoll_create(10);
    if (epollfd == -1) {
        perror("epoll_create");
        exit(EXIT_FAILURE);
    }

    ev.events = EPOLLIN;
    ev.data.fd = listen_sock;
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) {
        perror("epoll_ctl: listen_sock");
        exit(EXIT_FAILURE);
    }

    for (;;) {
        nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        if (nfds == -1) {
            close(listen_sock);
            perror("epoll_pwait");
            exit(EXIT_FAILURE);
        }

        for (n = 0; n < nfds; ++n) {
            if (events[n].data.fd == listen_sock) {
                conn_sock = accept(listen_sock,
                        (struct sockaddr *) &local, &addrlen);
                if (conn_sock == -1) {
                    perror("accept");
                    exit(EXIT_FAILURE);
                }
                setNonblocking(conn_sock);
                ev.events = EPOLLIN | EPOLLET;
                ev.data.fd = conn_sock;
                if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) {
                    perror("epoll_ctl: conn_sock");
                    exit(EXIT_FAILURE);
                }
            } else {
                conn_sock = events[n].data.fd;
                printf("%s:%d connected\n", inet_ntoa(local.sin_addr), ntohs(local.sin_port));
                send(conn_sock, buffer, recv(conn_sock, buffer, MAXBUF, 0), 0);
                if (epoll_ctl(epollfd, EPOLL_CTL_DEL, conn_sock, &ev) == -1) {
                    perror("epoll_ctl: conn_sock");
                    exit(EXIT_FAILURE);
                }
                close(conn_sock);
            }
        }
    }
}

以上是关于c_cpp epoll.c的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp server_tcp_epoll.c

IO 复用之 epoll

libevent源码分析:epoll后端实现

Redis_3.0 源代码文件分类

c_cpp 130.周围地区

c_cpp 200.岛屿数量