aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-05-01 18:23:30 -0400
committerBen Burwell <bburwell1@gmail.com>2013-05-01 18:23:30 -0400
commit27538e60a400c55410f80e0c2e9e767d5624c756 (patch)
tree06113ee8a00df5de72bdacaac13316cf4d917f4b
parent52baf39041c581b7dcbdf21c2249af189846d68e (diff)
Timeout on initiating end works!
-rw-r--r--chat_request.h35
1 files changed, 24 insertions, 11 deletions
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;