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