From 27538e60a400c55410f80e0c2e9e767d5624c756 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 1 May 2013 18:23:30 -0400 Subject: Timeout on initiating end works! --- chat_request.h | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'chat_request.h') diff --git a/chat_request.h b/chat_request.h index 6b4db03..38a3d7c 100644 --- a/chat_request.h +++ b/chat_request.h @@ -5,7 +5,6 @@ * Send chat request */ - void send_chat_request(char * username) { int i; @@ -50,22 +49,36 @@ void send_chat_request(char * username) { strcpy(out_buf, "SESREQ"); send(client_s, out_buf, strlen(out_buf), 0); + // set the timeout of the socket + struct timeval tv; + tv.tv_sec = REQUEST_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); - if (strcmp(in_buf, "SESANS:Y") == 0) { - printf("Request accepted, type /q to leave. \n"); + if (retcode == -1) { - // set my in_chat flag - strcpy(in_chat, "Y"); + // socket timed out or didn't receive anything + printf("No response from peer within timeout window \n"); - pthread_t recv_thread; - pthread_create(&recv_thread, NULL, receive_chat_messages, NULL); + } else { - // set the user prompt - strcpy(prompt, "chat>"); + if (strcmp(in_buf, "SESANS:Y") == 0) { + printf("Request accepted, type /q to leave. \n"); - } else { - printf("Session declined. \n"); + // set my in_chat flag + strcpy(in_chat, "Y"); + + pthread_t recv_thread; + pthread_create(&recv_thread, NULL, receive_chat_messages, NULL); + + // set the user prompt + strcpy(prompt, "chat>"); + + } else { + printf("Session declined. \n"); + } } return; -- cgit v1.2.3