From dd59e34110967d99e1479d8fcb0fb20ee47ce833 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 9 Jul 2021 22:44:29 +0200 Subject: [PATCH] Disconnection detection --- src/cli.c | 28 ++++++++++++++++++---------- src/server.c | 7 ++++--- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/cli.c b/src/cli.c index 7a349ec..a5e2e18 100644 --- a/src/cli.c +++ b/src/cli.c @@ -90,7 +90,7 @@ void connectedCommunication(int sockfd) int historyIndex = 0, historyModifier; char curChar; - int curLineLength, readLine; + int curLineLength, continueReadLine, answerLength; char *exitCommand = "exit"; char *promptString = "gem-graph console> "; @@ -102,15 +102,15 @@ void connectedCommunication(int sockfd) // Read command from terminal curLineLength = 0; - readLine = 1; + continueReadLine = 1; historyModifier = -1; - while (readLine && (curChar = getch())) { + while (continueReadLine && (curChar = getch())) { switch (curChar) { case '\n': printf("%c", curChar); - readLine = 0; + continueReadLine = 0; break; case KEY_DELETE: @@ -185,7 +185,7 @@ void connectedCommunication(int sockfd) } // Do not send null-string - if (sendBuff[0] == 0) + if (curLineLength == 0) continue; // Update history @@ -199,24 +199,30 @@ void connectedCommunication(int sockfd) } // Otherwise send command to server - write(sockfd, sendBuff, sizeof(sendBuff)); + send(sockfd, sendBuff, sizeof(sendBuff), 0); // Zeroing buffer bzero(receiveBuff, sizeof(receiveBuff)); // Reading server answer - read(sockfd, receiveBuff, sizeof(receiveBuff)); + answerLength = recv(sockfd, receiveBuff, sizeof(receiveBuff), 0); + + // Detect disconnection + if (answerLength == 0) { + printf("Disconnected\n"); + break; + } // Detect null-string returned - if (receiveBuff[0] == 0) { - printf("An error occured or invalid command!\n"); + if (receiveBuff[0] == '\0') { + printf("Invalid command!\n"); } printf("%s\n", receiveBuff); } } -int main() +int main(void) { int sockfd; struct sockaddr_in servaddr; @@ -247,4 +253,6 @@ int main() // close the socket close(sockfd); + + return 0; } diff --git a/src/server.c b/src/server.c index 4ffecb5..bfd42cb 100644 --- a/src/server.c +++ b/src/server.c @@ -33,7 +33,7 @@ static void *serverMain(void *server); /* -------------------------------------------------------------------------- */ // -------------------------------------------------------------------------- // -// Server init function // +// Server init function // // -------------------------------------------------------------------------- // void ServerInit(Server_t *server) { @@ -71,6 +71,7 @@ void *serverCommunicationInstance(void *server) if (bytesReceived == 0) break; + // Get ip address from client inet_ntop(AF_INET, &args->clientAddr.sin_addr, clientIP, @@ -103,13 +104,13 @@ void *serverCommunicationInstance(void *server) } // and send that buffer to client - send(args->sockfd, sendBuff, sizeof(sendBuff), 0); + send(args->sockfd, sendBuff, SEND_BUFFER_SIZE, 0); } close(args->sockfd); if (argv) free(argv); - printLog("End of communications \n"); + printLog("Disconnected from\n"); return NULL; }