Disconnection detection

This commit is contained in:
Adrien Bourmault 2021-07-09 22:44:29 +02:00
parent 8ae1f2f65f
commit dd59e34110
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
2 changed files with 22 additions and 13 deletions

View File

@ -90,7 +90,7 @@ void connectedCommunication(int sockfd)
int historyIndex = 0, historyModifier; int historyIndex = 0, historyModifier;
char curChar; char curChar;
int curLineLength, readLine; int curLineLength, continueReadLine, answerLength;
char *exitCommand = "exit"; char *exitCommand = "exit";
char *promptString = "gem-graph console> "; char *promptString = "gem-graph console> ";
@ -102,15 +102,15 @@ void connectedCommunication(int sockfd)
// Read command from terminal // Read command from terminal
curLineLength = 0; curLineLength = 0;
readLine = 1; continueReadLine = 1;
historyModifier = -1; historyModifier = -1;
while (readLine && (curChar = getch())) { while (continueReadLine && (curChar = getch())) {
switch (curChar) { switch (curChar) {
case '\n': case '\n':
printf("%c", curChar); printf("%c", curChar);
readLine = 0; continueReadLine = 0;
break; break;
case KEY_DELETE: case KEY_DELETE:
@ -185,7 +185,7 @@ void connectedCommunication(int sockfd)
} }
// Do not send null-string // Do not send null-string
if (sendBuff[0] == 0) if (curLineLength == 0)
continue; continue;
// Update history // Update history
@ -199,24 +199,30 @@ void connectedCommunication(int sockfd)
} }
// Otherwise send command to server // Otherwise send command to server
write(sockfd, sendBuff, sizeof(sendBuff)); send(sockfd, sendBuff, sizeof(sendBuff), 0);
// Zeroing buffer // Zeroing buffer
bzero(receiveBuff, sizeof(receiveBuff)); bzero(receiveBuff, sizeof(receiveBuff));
// Reading server answer // 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 // Detect null-string returned
if (receiveBuff[0] == 0) { if (receiveBuff[0] == '\0') {
printf("An error occured or invalid command!\n"); printf("Invalid command!\n");
} }
printf("%s\n", receiveBuff); printf("%s\n", receiveBuff);
} }
} }
int main() int main(void)
{ {
int sockfd; int sockfd;
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
@ -247,4 +253,6 @@ int main()
// close the socket // close the socket
close(sockfd); close(sockfd);
return 0;
} }

View File

@ -33,7 +33,7 @@ static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Server init function // // Server init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void ServerInit(Server_t *server) void ServerInit(Server_t *server)
{ {
@ -71,6 +71,7 @@ void *serverCommunicationInstance(void *server)
if (bytesReceived == 0) if (bytesReceived == 0)
break; break;
// Get ip address from client
inet_ntop(AF_INET, inet_ntop(AF_INET,
&args->clientAddr.sin_addr, &args->clientAddr.sin_addr,
clientIP, clientIP,
@ -103,13 +104,13 @@ void *serverCommunicationInstance(void *server)
} }
// and send that buffer to client // and send that buffer to client
send(args->sockfd, sendBuff, sizeof(sendBuff), 0); send(args->sockfd, sendBuff, SEND_BUFFER_SIZE, 0);
} }
close(args->sockfd); close(args->sockfd);
if (argv) if (argv)
free(argv); free(argv);
printLog("End of communications \n"); printLog("Disconnected from\n");
return NULL; return NULL;
} }