diff options
author | Ben Burwell <bburwell1@gmail.com> | 2013-04-29 16:13:34 -0400 |
---|---|---|
committer | Ben Burwell <bburwell1@gmail.com> | 2013-04-29 16:13:34 -0400 |
commit | ca9e46c937e75df43d2f80f0d957fabb07892c29 (patch) | |
tree | 0d8ae2bc1d1e3e248c746586a5daf7cb58a79463 /chat_request.h |
Init
Diffstat (limited to 'chat_request.h')
-rw-r--r-- | chat_request.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/chat_request.h b/chat_request.h new file mode 100644 index 0000000..8b29886 --- /dev/null +++ b/chat_request.h @@ -0,0 +1,106 @@ +/** + * Localchat + * Ben Burwell + * + * Send chat request + */ + +void send_chat_request(char * username) { + + int i; + char ip[16]; + + pthread_mutex_lock(&peer_table_lock); + + for (i = 0; i < num_peers_in_table; i++) { + if (strcmp(peers[i].username, username) == 0) { + strcpy(ip, peers[i].ip); + } + } + + pthread_mutex_unlock(&peer_table_lock); + + struct sockaddr_in server_addr; + char out_buf[4096]; + char in_buf[4096]; + int retcode; + + // create the socket + client_s = socket(AF_INET, SOCK_STREAM, 0); + + if (client_s < 0) { + printf("Network error, quitting... \n"); + exit(-1); + } + + // fill in server info + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(CHAT_PORT); + server_addr.sin_addr.s_addr = inet_addr(ip); + + retcode = connect(client_s, (struct sockaddr *)&server_addr, sizeof(server_addr)); + + if (retcode < 0) { + printf("Network error, quitting... \n"); + exit(-1); + } + + // send the session request + strcpy(out_buf, "SESREQ"); + send(client_s, out_buf, strlen(out_buf), 0); + + 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"); + + // set my in_chat flag + strcpy(in_chat, "Y"); + + // keep looping until no more input + int keep_going = 1; + + while (keep_going) { + + char inp[256]; + printf("chat> "); + fgets(inp, 140, stdin); + + if (strcmp(inp, "/q\n") == 0) { + + // send quit message to peer + strcpy(out_buf, "SESQ"); + send(client_s, out_buf, sizeof(out_buf), 0); + close(client_s); + + // exit the loop + keep_going = 0; + + // print message to user + printf("Session closed. \n"); + + // set my in_chat flag + strcpy(in_chat, "N"); + + } else { + + // send the message + strcpy(out_buf, "SESMSG:"); + strcat(out_buf, inp); + send(client_s, out_buf, sizeof(out_buf), 0); + + // testing... + printf("%s \n", out_buf); + + } + + } + + } else { + printf("Session declined. \n"); + } + + close(client_s); + + return; +}
\ No newline at end of file |