diff --git a/src/cli.c b/src/cli.c index a967e87..88ac647 100644 --- a/src/cli.c +++ b/src/cli.c @@ -33,20 +33,25 @@ void connectedCommunication(int sockfd) { - char buff[BUFFERSIZE]; + char buff[BUFFERSIZE], curChar; char *exitCommand = "exit"; - int n; + int i; for (;;) { // Zeroing buffer bzero(buff, sizeof(buff)); printf("\ngem-graph console> "); - n = 0; + i = 0; // Read command from terminal - while ((buff[n++] = getchar()) != '\n') - ; + while (( curChar= getchar()) != '\n') + buff[i++] = curChar; + + // Check null-sized string + if (buff[0] == 0) { + continue; + } // Quit if asked if (strcmp(exitCommand, buff) == 0) { diff --git a/src/server.c b/src/server.c index 1cd3fd2..a10d716 100644 --- a/src/server.c +++ b/src/server.c @@ -39,30 +39,33 @@ void ServerInit(Server_t *server) pthread_create(server->id, NULL, serverMain, server); } -#define BUFFER_SIZE 80 +#define SEND_BUFFER_SIZE 80 +#define RECEIVE_BUFFER_SIZE 80 static void serverCommunicationInstance(Server_t *args, int sockfd) { char **argv = NULL; - char *buff, *commandReturn; + char receiveBuff[RECEIVE_BUFFER_SIZE]; + char sendBuff[SEND_BUFFER_SIZE]; + char *commandReturn; int tokenIndex; //Accept and incoming connection while(!args->pleaseStop) { // Zeroing buffer - bzero(buff, BUFFER_SIZE); + bzero(receiveBuff, RECEIVE_BUFFER_SIZE); printLog("Waiting for incoming connections...\n"); // Read the message from client and copy it in buffer - read(sockfd, buff, sizeof(buff)); + read(sockfd, receiveBuff, sizeof(receiveBuff)); // Print buffer which contains the client request - printLog("Client request : %s\n", buff); + printLog("Client request : '%s'\n", receiveBuff); // get args in an array tokenIndex = 0; argv = (char**) realloc(argv, 1 * sizeof(char*)); - argv[0] = strtok(buff, " "); + argv[0] = strtok(receiveBuff, " "); while (argv[tokenIndex]) { tokenIndex++; argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); @@ -70,7 +73,7 @@ static void serverCommunicationInstance(Server_t *args, int sockfd) } // Zeroing buffer - bzero(buff, BUFFER_SIZE); + snprintf(sendBuff, SEND_BUFFER_SIZE, "%s", "Invalid command!"); // Execute command by first arg in cmdList for (int i = 0; i < LEN(cmdList); i++) { @@ -78,13 +81,13 @@ static void serverCommunicationInstance(Server_t *args, int sockfd) commandReturn = cmdList[i].execute(argv, args); // Copy server message in buffer - strcpy(buff, commandReturn); + strcpy(sendBuff, commandReturn); free(commandReturn); } } // and send that buffer to client - write(sockfd, buff, sizeof(buff)); + write(sockfd, sendBuff, sizeof(sendBuff)); } free(argv);