From be121ac22ff4925bbeceddf88eca12aa9142162e Mon Sep 17 00:00:00 2001 From: Igor Socec Date: Thu, 13 Mar 2014 11:02:19 +0100 Subject: [PATCH] fixing problems with detecting a disconnected client --- client.c | 5 +++++ moxerver.c | 24 ++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/client.c b/client.c index 53f99bc..4a6ff98 100644 --- a/client.c +++ b/client.c @@ -23,6 +23,11 @@ int client_read(struct client_t *client) { fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno)); return -errno; } + /* a disconnected client socket is ready for reading but read returns 0 */ + if (len == 0) { + fprintf(stderr, "[%s:%d] no data available from client\n", __func__, __LINE__); + return -ENODATA; //TODO document this in header + } //TODO let's print received bytes during development phase... { diff --git a/moxerver.c b/moxerver.c index c551b56..b17ba09 100644 --- a/moxerver.c +++ b/moxerver.c @@ -23,11 +23,11 @@ static void usage() { /* Performs cleanup and exit. */ void cleanup(int exit_code) { fprintf(stderr, "[%s] cleanup and exit with %d\n", NAME, exit_code); - /* close server and client */ - server_close(&server); + /* close client and server */ if (client.socket != -1) { client_close(&client); } + server_close(&server); exit(exit_code); } @@ -153,6 +153,13 @@ int main(int argc, char *argv[]) { if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) { /* read client data */ ret = client_read(&client); + /* check if client disconnected */ + if (ret == -ENODATA) { + fprintf(stderr, "[%s] client %s disconnected\n", NAME, client.ip_string); + /* close client connection and continue waiting for new clients */ + client_close(&client); + continue; + } if ( ret < 0) { /* print error but continue waiting for new data */ fprintf(stderr, "[%s] problem reading client\n", NAME); @@ -165,18 +172,7 @@ int main(int argc, char *argv[]) { } } if (ret == 0) { - /* check if client disconnected */ - /* a disconnected client socket is ready for reading but read returns 0 */ - if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) { - if (client_read(&client) == 0) { - fprintf(stderr, "[%s] client %s disconnected\n", NAME, client.ip_string); - /* close client connection */ - client_close(&client); - } - } - else { - fprintf(stderr, "[%s] server waiting\n", NAME); - } + fprintf(stderr, "[%s] server waiting\n", NAME); } } /* END while() loop */