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;
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;
}

View File

@ -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;
}