From 7d3df37b572192b6915f5fc3ea9803b195e3a5b3 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Mon, 14 Jul 2014 17:51:16 -0400 Subject: Minor fixes --- chatbot.c | 2 +- joshua.c | 6 +++--- server.c | 47 +++++++++++++++++++++++++++-------------------- util.c | 13 ++++++++----- util.h | 2 +- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/chatbot.c b/chatbot.c index 27bbdca..d48c432 100644 --- a/chatbot.c +++ b/chatbot.c @@ -104,7 +104,7 @@ void do_chatbot(void) print_string("\n\n"); print_string(exit_responses[rand()%(sizeof(exit_responses)/sizeof(const char*))]); print_string("\n--CONNECTION TERMINATED--"); - return; + exit(EXIT_SUCCESS); } } for(int i=0;i -#include -#include +#include "joshua.h" #include -#include #include -#include -#include -#include +#include #include -#include "joshua.h" +#include +#include +#include +#include +#include +#include + int server_socket; uint16_t port; int pipes[FD_SETSIZE][2]; @@ -58,9 +59,15 @@ int process_data(int fd) char buf[1024]; memset(buf, 0, sizeof(buf)); int ret=read(fd, buf, sizeof(buf)); + char ctrl_c[]={0xff, 0xf4, 0xff, 0xfd, 0x06, 0x00}; + if(strcmp(ctrl_c, buf)==0) + { + printf("Got CTRL-C from client.\n"); + return -1; + } if(ret<0) /* error */ { - printf("error in read()\n"); + printf("Error in read()\n"); return -1; } if(ret==0) @@ -80,7 +87,6 @@ void serv_cleanup() printf("preparing to exit...\n"); fflush(stdout); shutdown(server_socket, SHUT_RDWR); - close(server_socket); } int main(int argc, char* argv[]) { @@ -89,8 +95,8 @@ int main(int argc, char* argv[]) printf("Usage: %s PORT\n", argv[0]); return 2; } - printf("starting server...\n"); port=atoi(argv[1]); + printf("Initializing server on port %u...\n", port); signal(SIGINT, &serv_cleanup); int sock=make_server_socket(port); server_socket=sock; @@ -98,19 +104,19 @@ int main(int argc, char* argv[]) struct sockaddr_in client; if(listen(sock, 1)<0) { - printf("error listening.\n"); + printf("Error opening socket.\n"); return 1; } FD_ZERO(&active_fd_set); FD_SET(sock, &active_fd_set); - printf("listening on port %d\n", port); + printf("Server ready, listening on port %d.\n", port); while(1) { read_fd_set=active_fd_set; int ret=select(FD_SETSIZE, &read_fd_set, 0,0,0); if(ret<0) { - printf("select() returned error.\n"); + printf("Error in select().\n"); return 1; } for(int i=0;i=0) + /* remove the newline */ + buf[strlen(buf)-2]='\0'; } - printf("got string \"%s\"\n", buf); if(ret<0) return ERR; return OK; diff --git a/util.h b/util.h index 003f8e7..5f7e170 100644 --- a/util.h +++ b/util.h @@ -14,7 +14,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + * * Contact the author at contact@fwei.tk */ -- cgit v1.1