forked from scott/threaded_network_chat
138 lines
3.5 KiB
C++
138 lines
3.5 KiB
C++
#include <arpa/inet.h>
|
|
#include <curses.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <ostream>
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <string>
|
|
#include <sys/socket.h>
|
|
#include <sys/time.h>
|
|
#include <sys/types.h>
|
|
#include <sys/uio.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include <vector> //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
|
|
|
|
int mode = 0; // 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<int> 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 && argv[1][0] == 'c') {
|
|
// client mode
|
|
logFileName = "client.txt";
|
|
mode = 2;
|
|
} else {
|
|
// server mode
|
|
logFileName = "server.txt";
|
|
mode = 1;
|
|
}
|
|
|
|
// 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;
|
|
|
|
if (mode == 2) {
|
|
writeToFile(logFileName, "CLIENT MODE");
|
|
setupClient();
|
|
} else if (mode == 1) {
|
|
writeToFile(logFileName, "SERVER MODE");
|
|
setupServer(PORT_NUM);
|
|
linePos++; /* No idea why, but this needs to be here. */
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
endwin();
|
|
//closeServer();
|
|
|
|
if (mode == 1)
|
|
closeServer();
|
|
else if (mode == 2)
|
|
closeClient();
|
|
|
|
return 0;
|
|
}
|