From 9a6fadd558904dda8a5fe68bc216f81ab37987bb Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 11 Feb 2019 17:04:12 +0100 Subject: [PATCH] Fix listen and remove debug --- src/donar_server.c | 10 ++-------- src/net_tools.c | 4 ++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/donar_server.c b/src/donar_server.c index 31880db..42aaefd 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -56,20 +56,16 @@ void handle_new_tcp_client_connection(struct donar_server_ctx* ctx, struct epoll socklen_t in_len; struct epoll_event current_event; - printf("Accepting new TCP client\n"); in_len = sizeof(in_addr); conn_sock = accept(evt->data.fd, &in_addr, &in_len); if (conn_sock == -1) goto co_error; - printf("accept %d\n", conn_sock); err = make_socket_non_blocking (conn_sock); if (err == -1) goto co_error; - printf("make socket non blocking\n"); current_event.events = EPOLLIN | EPOLLET; current_event.data.fd = conn_sock; if (epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, conn_sock, ¤t_event) == -1) goto co_error; - printf("add to epoll socket descriptor %d\n", conn_sock); return; co_error: @@ -80,13 +76,11 @@ co_error: void handle_new_tcp_client_rw(struct donar_server_ctx* ctx, struct epoll_event* evt) { char buffer[128]; int nread, nwrite; - printf("Exchanging rw info with client for sock %d\n", evt->data.fd); while(1) { nread = read(evt->data.fd, buffer, sizeof(buffer)); if (nread == 0) break; if (nread == -1 && errno == EAGAIN) break; if (nread == -1) goto co_error; - printf("successful read of %d", nread); nwrite = write(evt->data.fd, buffer, nread); if (nwrite == -1 && errno == EAGAIN) { printf("Lost data EAGAIN\n"); @@ -132,7 +126,7 @@ void donar_server(struct donar_server_ctx* ctx) { } } - printf("Start main loop\n"); + printf("--- Start main loop\n"); int num_fd, n = 0; while(1) { num_fd = epoll_wait(ctx->epollfd, events, MAX_EVENTS, -1); @@ -140,7 +134,7 @@ void donar_server(struct donar_server_ctx* ctx) { perror("Failed to epoll_wait"); exit(EXIT_FAILURE); } - printf("Loop with %d elements\n", num_fd); + for (n = 0 ; n < num_fd; n++) { if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (!(events[n].events & EPOLLIN))) { /* An error has occured on this fd, or the socket is not diff --git a/src/net_tools.c b/src/net_tools.c index d9c0365..779bf3c 100644 --- a/src/net_tools.c +++ b/src/net_tools.c @@ -43,9 +43,9 @@ int create_tcp_server(char* service) { struct addrinfo *result, *cursor; memset(&conf, 0, sizeof(struct addrinfo)); - conf.ai_family = AF_UNSPEC; + conf.ai_family = AF_INET; // AF_UNSPEC to listen on IPv6 or IPv4 conf.ai_socktype = SOCK_STREAM; - conf.ai_flags = AI_PASSIVE; + conf.ai_flags = 0; // AI_PASSIVE to listen on 0.0.0.0 conf.ai_protocol = 0; err = getaddrinfo(NULL, service, &conf, &result);