#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //try to make the program work without this without doing anything awful but do it later #include "client.h" #include "disp.h" #include "log.h" #include "public.h" #include "server.h" using namespace std; const int PORT_NUM = 8888; string IP_ADDRESS = "127.0.0.1"; const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor // position for message entry const int DEFAULT_CUR_Y = 12; // the x position of the preferred default cursor // position for message entry chatmode_t mode = NO_MODE; // what mode is this program in. 0 = nothing. 1 = server. 2 = client int serverSocketDescriptor; // a global variable for storing the server socket // descriptor. int clientSocketDescriptor; vector clientSocketDescriptors; // a global vector for storing client // socket descriptors // keep track of the session time using global variables struct timeval start1, end1; // also keep track of the amount of data sent as well int bytesRead, bytesWritten = 0; const int LOG_LENGTH = 10; // number of text log file lines to display int linePos = 0; // counter for what current position to use in the file string logFileName; // the name of the log file pthread_t client_wait_thread; struct waitClientArgs { sockaddr_in newSockAddr; socklen_t newSockAddrSize; int auxInt; // used in client mode because I didn't want to make another // struct to keep track of }; int main(int argc, char *argv[]) { if (argc > 1 && (!strncmp(argv[1], "client", 6))) { logFileName = "client.txt"; mode = CLIENT_MODE; } else { logFileName = "server.txt"; mode = SERVER_MODE; } // putting this lower down to circumvent the terminal brick when the // socket was already in use results in all network functonality no // longer working. initscr(); // creates stdscr. important step use_default_colors(); // this apparently tells it to use default // terminal colors whenever there is no color // attribute applied // printw("Starting server..."); // you have to do this to make the scrolling still work correctly if // there was already content in the log file linePos = linesInFile(logFileName) - LOG_LENGTH + 1; char *userInput = new char[1024]; bool exit = false; switch (mode) { case CLIENT_MODE: writeToFile(logFileName, "CLIENT MODE"); setupClient(); linePos++; break; case SERVER_MODE: writeToFile(logFileName, "SERVER MODE"); setupServer(PORT_NUM); linePos++; break; default: goto leave; break; } while (!exit) { displayFile(logFileName, linePos, LOG_LENGTH); // scroll along the screen if and when required so that it stays // in sync if (linesInFile(logFileName) > LOG_LENGTH) linePos++; /* clear message box / reset cursor */ move(12, 0); 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 send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0); } leave: endwin(); // closeServer(); switch (mode) { case SERVER_MODE: closeServer(); break; case CLIENT_MODE: closeServer(); break; default: puts("Warn: program appears to have successfully finished without ever setting " "mode."); return 1; break; } return 0; }