forked from scott/threaded_network_chat
fix logs being spammed with newlines after other party ends session
This commit is contained in:
19
client.cc
19
client.cc
@@ -24,10 +24,12 @@ closeClient()
|
||||
|
||||
gettimeofday(&end1, NULL);
|
||||
close(clientSocketDescriptor);
|
||||
printf("********Session********");
|
||||
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.");
|
||||
}
|
||||
|
||||
void *
|
||||
@@ -37,6 +39,7 @@ pollForSever(void *args)
|
||||
int socketDescriptor = (int)aaa->auxInt;
|
||||
char msg[1024];
|
||||
while (1) {
|
||||
server_message_loop:
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead += recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
|
||||
@@ -50,12 +53,18 @@ pollForSever(void *args)
|
||||
// cout << "Server: " << msg << endl;
|
||||
// printf("Server: %s\n");
|
||||
|
||||
if (msg[0] == '\0')
|
||||
goto server_message_loop;
|
||||
|
||||
writeToFile(logFileName, msg);
|
||||
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -82,7 +91,7 @@ setupClient()
|
||||
|
||||
if (status < 0)
|
||||
writeToFile(logFileName, "Error connecting to socket!");
|
||||
|
||||
|
||||
writeToFile(logFileName, "Connected to the server!");
|
||||
int bytesRead, bytesWritten = 0;
|
||||
struct timeval start1, end1;
|
||||
|
||||
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