aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-05-01 20:35:47 -0400
committerBen Burwell <bburwell1@gmail.com>2013-05-01 20:35:47 -0400
commit61696c62d19356ed6133887ebb947e6e1f0a1467 (patch)
treee2a42fa4e4a533ea5e4fb646b9e6f00ec9c46a12
parentb5e98849be10e0a089a0e4642d0c25a0dc0952f7 (diff)
Adds chat session timeout
-rw-r--r--chat_acceptor.h15
1 files changed, 14 insertions, 1 deletions
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);