You are not logged in.

#1 2009-10-19 04:43:47

blakeman8192
Member
From: Earth
Registered: 2009-09-19
Posts: 7

Need some assistance with epoll

Hey guys, I've been having some problems with epoll so I thought I'd come to you. tongue

First off, here's my code:

/*
 * OpenRS main server class.
 *
 * author: blakeman8192
 */

#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>

#include "server.h"

int main()
{
    printf("OpenRS server v1.0\n");
    printf("Released under the GPL License.\n");
    printf("Copyright 2009 Blake Beaupain.\n");
    printf("------------------------------------------------------\n");
    printf("Starting server...\n");
    startserver();
    printf("------------------------------------------------------\n");
    printf("Shutting down.\n");
    return 0;
}

/*
 * Starts server operation.
 */
void startserver()
{
    struct epoll_event ev, events[MAX_EVENTS];
    struct sockaddr_in address;
    int listen_sock, client_sock, nfds, epollfd;
    
    /*
     * Initialize the listener socket.
     */
    listen_sock = socket(AF_INET, SOCK_STREAM, 0);
    fcntl(listen_sock, F_SETFL, (fcntl(listen_sock, F_GETFL, 0) | O_NONBLOCK));
    
    /*
     * Configure the listener address.
     */
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);
    int addrlen = sizeof(address);
    
    /*
     * Bind the listener address to the listener socket.
     */
    printf("\tBinding listener_sock on port: %d", PORT);
    if (bind(listen_sock, (struct sockaddr *) &address, addrlen) == -1)
    {
        printf("\t[FAIL]\n");
        perror("\tbind: listen_sock");
        return;
    }
    printf("\t[OK]\n");
    
    /*
     * Initialize the epoll file descriptor.
     */
    printf("\tInitializing epoll file descriptor...");
    epollfd = epoll_create(MAX_CONNECTIONS);
    if (epollfd == -1)
    {
        printf("\t[FAIL]\n");
        perror("\tepoll_create");
        return;
    }
    printf("\t[OK]\n");
    
    /*
     * Configure the epoll file descriptor.
     */
    ev.events = EPOLLIN;
    ev.data.fd = listen_sock;
    
    /*
     * Register the listener socket with the epoll file descriptor.
     */
    printf("\tRegistering listener with epoll...");
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1)
    {
        printf("\t[FAIL]\n");
        perror("\tepoll_ctl: listen_sock");
        return;
    }
    printf("\t[OK]\n");
    printf("------------------------------------------------------\n");
    printf("OpenRS server running.\n");
    
    /*
     * Begin polling for IO events.
     */
    for (;;)
    {
        /*
         * Wait for an event to become ready.
         */
        nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        if (nfds == -1)
        {
            perror("epoll_wait");
            return;
        }
        
        /*
         * Iterate through and handle each event.
         */
        int n;
        for (n = 0; n < nfds; n++)
        {
            /*
             * Accept any in-bound connections.
             */
            if (events[n].data.fd == listen_sock)
            {
                /*
                 * Accept the connection.
                 */
                client_sock = accept(listen_sock, (struct sockaddr *) &address, &addrlen);
                if (client_sock == -1)
                {
                    perror("accept");
                    continue;
                }
                fcntl(client_sock, fcntl(client_sock, F_GETFL, 0) | O_NONBLOCK);
            }
            
        }
    }
}

My problem is that when I run the program, I get spammed with

accept: Invalid argument

Obviously it will "spam" it because of the loop, but
1) Why is epoll not waiting like it should indefinitely for an accept event to be ready?
2) Why am I getting invalid argument for the accept call?


Thanks for your help guys,
-Blake

Last edited by blakeman8192 (2009-10-19 04:45:36)

Offline

Board footer

Powered by FluxBB