Disconnection detection
This commit is contained in:
parent
8ae1f2f65f
commit
dd59e34110
28
src/cli.c
28
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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue