Compare commits

..

7 Commits

5 changed files with 57 additions and 16 deletions

View File

@@ -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);
@@ -49,11 +52,19 @@ 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
@@ -77,8 +88,10 @@ setupClient()
// try to connect...
int status =
connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
if (status < 0)
writeToFile(logFileName, "Error connecting to socket!");
writeToFile(logFileName, "Connected to the server!");
int bytesRead, bytesWritten = 0;
struct timeval start1, end1;

View File

@@ -1,2 +0,0 @@
g++ -pthread -Wall -Wextra -O2 -std=c++17 -lcurses -g -c *.cc
g++ -pthread -Wall -Wextra -O2 -std=c++17 -lcurses -g -o ct *.o

11
log.cc
View File

@@ -1,5 +1,8 @@
#include <fstream>
#include <ios>
#include <sys/stat.h>
#include "public.h"
#include "log.h"
@@ -27,10 +30,16 @@ int
writeToFile(string path, string line, bool incLineNum)
{
ofstream file;
file.open(path, ios_base::app); // open the file in append mode
/*
* Hopefully this fixes the program messing up where the server/client
* doesn't start if the file wasn't created by a previous session.
*/
file.open(path, ios_base::out|ios_base::app);
if (file.is_open()) {
file << line << endl;
file.close();
// this probably would help but theres too much broken stuff
// right now to be sure if (incLineNum)
//{

15
main.cc
View File

@@ -97,6 +97,7 @@ main(int argc, char *argv[])
} else if (mode == 1) {
writeToFile(logFileName, "SERVER MODE");
setupServer(PORT_NUM);
linePos++; /* No idea why, but this needs to be here. */
}
while (!exit) {
@@ -107,10 +108,17 @@ main(int argc, char *argv[])
if (linesInFile(logFileName) > LOG_LENGTH)
linePos++;
/* clear message box / reset cursor */
move(12, 0);
printw("> ");
printw(">\t\t\t");
move(12,2);
getstr(userInput);
writeToFile(logFileName, userInput);
if (!strncmp(userInput, "/quit", 5))
exit = true;
if (mode == 1)
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
else
@@ -118,7 +126,12 @@ main(int argc, char *argv[])
}
endwin();
//closeServer();
if (mode == 1)
closeServer();
else if (mode == 2)
closeClient();
return 0;
}

View File

@@ -1,3 +1,4 @@
#include <cstdio>
#include <curses.h>
#include <netinet/in.h>
#include <pthread.h>
@@ -40,13 +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);
}
@@ -98,7 +105,7 @@ setupServer(int port)
aaa->newSockAddrSize = newSockAddrSize;
int rc = pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
pthread_detach(client_wait_thread);
writeToFile(logFileName, "Server started sucessfully");
writeToFile(logFileName, "Server started successfully");
gettimeofday(&start1, NULL);
return 0;
@@ -116,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.");
}