From 61696c62d19356ed6133887ebb947e6e1f0a1467 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 1 May 2013 20:35:47 -0400 Subject: Adds chat session timeout --- chat_acceptor.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/chat_acceptor.h b/chat_acceptor.h index a67374b..83d0d51 100644 --- a/chat_acceptor.h +++ b/chat_acceptor.h @@ -13,6 +13,12 @@ void *receive_chat_messages(void *arg) { char separators[4] = ":"; char * tok; + // set the timeout of the socket + struct timeval tv; + tv.tv_sec = SESSION_TIMEOUT; + tv.tv_usec = 0; + setsockopt(client_s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); + retcode = recv(client_s, in_buf, sizeof(in_buf), 0); while (strcmp(in_buf, "SESQ") != 0) { @@ -27,6 +33,8 @@ void *receive_chat_messages(void *arg) { fflush(stdout); } + } else { + break; } strcpy(in_buf, ""); @@ -34,7 +42,12 @@ void *receive_chat_messages(void *arg) { retcode = recv(client_s, in_buf, sizeof(in_buf), 0); } - printf("\n*** Peer closed the session. *** \n"); + // display appropriate error message + if (retcode == -1) { + printf("\n*** Session timed out. *** \n"); + } else { + printf("\n*** Peer closed the session. *** \n"); + } // close the socket close(client_s); -- cgit v1.2.3