From 2ae315d33af408e2ada1307c1af54aa249cf34c7 Mon Sep 17 00:00:00 2001 From: "jazz (gitea)" Date: Wed, 4 Mar 2026 15:19:42 -0600 Subject: [PATCH] remove unnecessary fcntl.h, add ostream, replace cout/cerror with print statements, remove outdated comments --- main.cpp | 102 ++++++++++++------------------------------------------- 1 file changed, 22 insertions(+), 80 deletions(-) diff --git a/main.cpp b/main.cpp index 830b3fe..9680e28 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ /*an attempt at pthread and network and ncurses all in the same function */ #include +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include #include @@ -89,7 +89,7 @@ int displayFile(string path, int startLineNum = 0, int numLines = 10) if (num >= startLineNum) { move(lineNum, 0); - printw(line.c_str()); + printw("%s", line.c_str()); lineNum++;//increment the row number after printing each line } num++; @@ -184,7 +184,7 @@ int setupServer(int port) //keeps from bricking the terminal if this happens endwin(); - cerr << "Error establishing the server socket" << endl; + fprintf(stderr, "Error establishing the server socket!\n"); exit(0); } //bind the socket to its local address @@ -195,7 +195,7 @@ int setupServer(int port) //keeps from bricking the terminal if this happens endwin(); - cerr << "Error binding socket to local address" << endl; + fprintf(stderr, "Error binding socket to local address!\n"); exit(0); } writeToFile(logFileName, "Waiting for a client to connect..."); @@ -215,59 +215,35 @@ int setupServer(int port) writeToFile(logFileName, "Server started sucessfully"); gettimeofday(&start1, NULL); - /*while(1) - { - //receive a message from the client (listen) - cout << "Awaiting client response..." << endl; - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(clientSocketDescriptor, (char*)&msg, sizeof(msg), 0); - if(!strcmp(msg, "exit")) - { - cout << "Client has quit the session" << endl; - break; - } - cout << "Client: " << msg << endl; - cout << ">"; - string data; - getline(cin, data); - memset(&msg, 0, sizeof(msg)); //clear the buffer - strcpy(msg, data.c_str()); - if(data == "exit") - { - //send to the client that server has closed the connection - send(clientSocketDescriptor, (char*)&msg, strlen(msg), 0); - break; - } - //send the message to client - bytesWritten += send(clientSocketDescriptor, (char*)&msg, strlen(msg), 0); - }*/ - - return 0; + return 0; } void closeServer() { + long e = end1.tv_sec - start1.tv_sec; /* the linter freaks out if you don't save it as a variable */ + //we need to close the socket descriptors after we're all done gettimeofday(&end1, NULL); close(clientSocketDescriptor); close(serverSocketDescriptor); - cout << "********Session********" << endl; - cout << "Bytes written: " << bytesWritten << " Bytes read: " << bytesRead << endl; - cout << "Elapsed time: " << (end1.tv_sec - start1.tv_sec) - << " secs" << endl; - cout << "Connection closed..." << endl; + + /* 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"); } void closeClient() { + long e = end1.tv_sec - start1.tv_sec; + gettimeofday(&end1, NULL); close(clientSocketDescriptor); - cout << "********Session********" << endl; - cout << "Bytes written: " << bytesWritten << - " Bytes read: " << bytesRead << endl; - cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec) - << " secs" << endl; - cout << "Connection closed" << endl; + printf("********Session********"); + printf("Bytes written: %i\nBytes read: %i\n", bytesWritten, bytesRead); + printf("Elapsed time: %ld\n secs\n", e); + printf("Connection closed...\n"); } void* pollForSever(void* args) @@ -277,18 +253,6 @@ void* pollForSever(void* args) char msg[1024]; while(1) { - /*string data; - getline(cin, data); - memset(&msg, 0, sizeof(msg));//clear the buffer - strcpy(msg, data.c_str()); - if(data == "exit") - { - send(*socketDescriptor, (char*)&msg, strlen(msg), 0); - break; - }*/ - //bytesWritten += send(*socketDescriptor, (char*)&msg, strlen(msg), 0); - //writeToFile(logFileName, msg); - //displayFile(logFileName, linePos, LOG_LENGTH); memset(&msg, 0, sizeof(msg));//clear the buffer bytesRead += recv(socketDescriptor, (char*)&msg, sizeof(msg), 0); @@ -300,6 +264,7 @@ void* pollForSever(void* args) break; }*/ //cout << "Server: " << msg << endl; + //printf("Server: %s\n"); writeToFile(logFileName, msg); if (linesInFile(logFileName) > LOG_LENGTH) { @@ -342,29 +307,6 @@ void setupClient() aaa->auxInt = clientSocketDescriptor; int rc = pthread_create(&client_wait_thread, nullptr, pollForSever, aaa); pthread_detach(client_wait_thread); - /*while(1)s - { - cout << ">"; - string data; - getline(cin, data); - memset(&msg, 0, sizeof(msg));//clear the buffer - strcpy(msg, data.c_str()); - if(data == "exit") - { - send(clientSd, (char*)&msg, strlen(msg), 0); - break; - } - bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0); - cout << "Awaiting server response..." << endl; - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0); - if(!strcmp(msg, "exit")) - { - cout << "Server has quit the session" << endl; - break; - } - cout << "Server: " << msg << endl; - }*/ } //appends a line of text to the end of a given file. returns 0 if the file existed, 1 if it didn't work @@ -392,7 +334,6 @@ int writeToFile(string path, string line, bool incLineNum) int main(int argc, char *argv[]) { - //doesnt fucking work if (argc > 1 && argv[1][0] == 'c') { //client mode @@ -457,4 +398,5 @@ int main(int argc, char *argv[]) closeServer(); return 0; -} \ No newline at end of file +} +