示例#1
0
int main(int argc, const char *argv[])
{
    int i = 0;
    size_t client_capacity = 100;
    size_t total_capacity = 101; //+1 extra for server socket

    int srv_fd = -1;
    struct sockaddr_in srv_addr;

    int epoll_fd = -1;
    struct epoll_event e, *es;

    if (argc == 2) {
    	client_capacity = atoi(argv[1]); //read capacity from program argument list
    	total_capacity = client_capacity + 1;
    }

    USER_LIST = create_ul(client_capacity);

    memset(&srv_addr, 0, sizeof(srv_addr));
    memset(&e, 0, sizeof(e));

    es = malloc((total_capacity) * sizeof(struct epoll_event));

    srv_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
    if (srv_fd < 0) {
        printf("Cannot create socket\n");
        return 1;
    }

    srv_addr.sin_family = AF_INET;
    srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    srv_addr.sin_port = htons(5553);
    if (bind(srv_fd, (struct sockaddr*) &srv_addr, sizeof(srv_addr)) < 0) {
        printf("Cannot bind socket\n");
        close(srv_fd);
        return 1;
    }

    if (listen(srv_fd, total_capacity) < 0) {
        printf("Cannot listen\n");
        close(srv_fd);
        return 1;
    }

    epoll_fd = epoll_create(total_capacity);
    if (epoll_fd < 0) {
        printf("Cannot create epoll\n");
        close(srv_fd);
        return 1;
    }

    e.events = EPOLLIN;
    e.data.fd = srv_fd;
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, srv_fd, &e) < 0) {
        printf("Cannot add server socket to epoll\n");
        close(epoll_fd);
        close(srv_fd);
        return 1;
    }

    for(;;) {
        i = epoll_wait(epoll_fd, es, total_capacity, -1);
        if (i < 0) {
            printf("Cannot wait for events\n");
            close(epoll_fd);
            close(srv_fd);
            return 1;
        }

        for (--i; i > -1; --i) {
            if (es[i].data.fd == srv_fd) {
                if (server_service(srv_fd, epoll_fd)) {
                	return 1;
                }
            } else {
            	client_service(es[i].data.fd, es[i].events, epoll_fd);
            }
        }
    }

	return 0;
}
示例#2
0
文件: main.c 项目: emiliancyc/epoll
int main(int argc, const char *argv[]) {

	if (argc != 2) {
		printf("Zla liczba argumentow! Podaj tylko jeden!\n");
		return -1;
	}

	time_t t;
	srand((unsigned) time(&t));
	unsigned short int port = ((rand() % 500) + 8000);
	printf("%d\n", port);

	int CNT = atoi(argv[1]);

	struct sockaddr_in srv_addr;
	struct epoll_event e, es[CNT]; //e dany event  es[] tablica eventow do obslugi epoll-wielu klientow
	memset(&srv_addr, 0, sizeof(srv_addr)); // memset zeruje nam te strukturki
	memset(&e, 0, sizeof(e));

	srv_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
	if (srv_fd < 0) {
		printf("Cannot create socket\n");
		return 1;
	}

	srv_addr.sin_family = AF_INET;
	srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	srv_addr.sin_port = htons(port);

	if (bind(srv_fd, (struct sockaddr*) &srv_addr, sizeof(srv_addr)) < 0) {
		printf("Cannot bind socket\n");
		close(srv_fd);
		return 1;
	}

	if (listen(srv_fd, 1) < 0) {
		printf("Cannot listen\n");
		close(srv_fd);
		return 1;
	}

	epoll_fd = epoll_create(2);
	if (epoll_fd < 0) {
		printf("Cannot create epoll\n");
		close(srv_fd);
		return 1;
	}

	e.events = EPOLLIN;
	e.data.fd = srv_fd;
	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, srv_fd, &e) < 0) {
		printf("Cannot add server socket to epoll\n");
		close(epoll_fd);
		close(srv_fd);
		return 1;
	}

	size_t cap = CNT;
	user_list* lista = create_ul(cap);

	int i = 0;
	for (;;) {
		i = epoll_wait(epoll_fd, es, CNT, -1);

		if (i < 0) {
			printf("Cannot wait for events\n");
			close(epoll_fd);
			close(srv_fd);
			return 1;
		}

		for (--i; i > -1; --i) {
			if (es[i].data.fd == srv_fd) { //pobiera nam deskryptor i jesli to bedzie deskryptor serwera, to wchodzi w if'a
				if (accept_new_client() != 0)
					return 1; //shut down server
			} else {
				serve_client(es[i].data.fd, es[i].events, lista);
			}
		}
	}

	return 0;
}