forked from scott/threaded_network_chat
fix logs being spammed with newlines after other party ends session
This commit is contained in:
19
server.cc
19
server.cc
@@ -1,3 +1,4 @@
|
||||
#include <cstdio>
|
||||
#include <curses.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
@@ -40,14 +41,19 @@ pollForClient()
|
||||
{
|
||||
char msg[1024];
|
||||
while (1) {
|
||||
client_message_loop:
|
||||
// receive a message from the client (listen)
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead += recv(clientSocketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
|
||||
writeToFile(logFileName,
|
||||
msg); // write the client's message to file
|
||||
if (msg[0] == '\0')
|
||||
goto client_message_loop;
|
||||
|
||||
writeToFile(logFileName, msg);
|
||||
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
}
|
||||
|
||||
@@ -117,8 +123,9 @@ closeServer()
|
||||
close(serverSocketDescriptor);
|
||||
|
||||
/* Don't just die silently */
|
||||
printf("********Session********\n");
|
||||
printf("Bytes written: %i\nBytes Read: %i\n", bytesWritten, bytesRead);
|
||||
printf("Elapsed time: %ld\n secs\n", e);
|
||||
printf("Connection closed...\n");
|
||||
puts("********Session********");
|
||||
printf("Bytes written: %i\n", bytesWritten);
|
||||
printf("Bytes read: %i\n", bytesRead);
|
||||
printf("Elapsed time: %ld secs\n", e);
|
||||
puts("Connection closed.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user