forked from scott/threaded_network_chat
Compare commits
7 Commits
9625ab2881
...
099ec012a2
| Author | SHA1 | Date | |
|---|---|---|---|
| 099ec012a2 | |||
| bfe1ef98d4 | |||
| 99a88bca46 | |||
| 3810db7ac8 | |||
| 192e2f69ee | |||
| a054041351 | |||
| 0aa7a07c8b |
21
client.cc
21
client.cc
@@ -24,10 +24,12 @@ closeClient()
|
|||||||
|
|
||||||
gettimeofday(&end1, NULL);
|
gettimeofday(&end1, NULL);
|
||||||
close(clientSocketDescriptor);
|
close(clientSocketDescriptor);
|
||||||
printf("********Session********");
|
|
||||||
printf("Bytes written: %i\nBytes read: %i\n", bytesWritten, bytesRead);
|
puts("********Session********");
|
||||||
printf("Elapsed time: %ld\n secs\n", e);
|
printf("Bytes written: %i\n", bytesWritten);
|
||||||
printf("Connection closed...\n");
|
printf("Bytes read: %i\n", bytesRead);
|
||||||
|
printf("Elapsed time: %ld secs\n", e);
|
||||||
|
puts("Connection closed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
@@ -37,6 +39,7 @@ pollForSever(void *args)
|
|||||||
int socketDescriptor = (int)aaa->auxInt;
|
int socketDescriptor = (int)aaa->auxInt;
|
||||||
char msg[1024];
|
char msg[1024];
|
||||||
while (1) {
|
while (1) {
|
||||||
|
server_message_loop:
|
||||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||||
bytesRead += recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
bytesRead += recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||||
|
|
||||||
@@ -49,11 +52,19 @@ pollForSever(void *args)
|
|||||||
}*/
|
}*/
|
||||||
// cout << "Server: " << msg << endl;
|
// cout << "Server: " << msg << endl;
|
||||||
// printf("Server: %s\n");
|
// printf("Server: %s\n");
|
||||||
|
|
||||||
|
if (msg[0] == '\0')
|
||||||
|
goto server_message_loop;
|
||||||
|
|
||||||
writeToFile(logFileName, msg);
|
writeToFile(logFileName, msg);
|
||||||
|
|
||||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||||
linePos++;
|
linePos++;
|
||||||
|
|
||||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -77,8 +88,10 @@ setupClient()
|
|||||||
// try to connect...
|
// try to connect...
|
||||||
int status =
|
int status =
|
||||||
connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
||||||
|
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
writeToFile(logFileName, "Error connecting to socket!");
|
writeToFile(logFileName, "Error connecting to socket!");
|
||||||
|
|
||||||
writeToFile(logFileName, "Connected to the server!");
|
writeToFile(logFileName, "Connected to the server!");
|
||||||
int bytesRead, bytesWritten = 0;
|
int bytesRead, bytesWritten = 0;
|
||||||
struct timeval start1, end1;
|
struct timeval start1, end1;
|
||||||
|
|||||||
2
com.txt
2
com.txt
@@ -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
11
log.cc
@@ -1,5 +1,8 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <ios>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "public.h"
|
#include "public.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
@@ -27,10 +30,16 @@ int
|
|||||||
writeToFile(string path, string line, bool incLineNum)
|
writeToFile(string path, string line, bool incLineNum)
|
||||||
{
|
{
|
||||||
ofstream file;
|
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()) {
|
if (file.is_open()) {
|
||||||
file << line << endl;
|
file << line << endl;
|
||||||
|
file.close();
|
||||||
// this probably would help but theres too much broken stuff
|
// this probably would help but theres too much broken stuff
|
||||||
// right now to be sure if (incLineNum)
|
// right now to be sure if (incLineNum)
|
||||||
//{
|
//{
|
||||||
|
|||||||
17
main.cc
17
main.cc
@@ -97,6 +97,7 @@ main(int argc, char *argv[])
|
|||||||
} else if (mode == 1) {
|
} else if (mode == 1) {
|
||||||
writeToFile(logFileName, "SERVER MODE");
|
writeToFile(logFileName, "SERVER MODE");
|
||||||
setupServer(PORT_NUM);
|
setupServer(PORT_NUM);
|
||||||
|
linePos++; /* No idea why, but this needs to be here. */
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!exit) {
|
while (!exit) {
|
||||||
@@ -107,10 +108,17 @@ main(int argc, char *argv[])
|
|||||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||||
linePos++;
|
linePos++;
|
||||||
|
|
||||||
|
/* clear message box / reset cursor */
|
||||||
move(12, 0);
|
move(12, 0);
|
||||||
printw("> ");
|
printw(">\t\t\t");
|
||||||
|
move(12,2);
|
||||||
|
|
||||||
getstr(userInput);
|
getstr(userInput);
|
||||||
writeToFile(logFileName, userInput);
|
writeToFile(logFileName, userInput);
|
||||||
|
|
||||||
|
if (!strncmp(userInput, "/quit", 5))
|
||||||
|
exit = true;
|
||||||
|
|
||||||
if (mode == 1)
|
if (mode == 1)
|
||||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||||
else
|
else
|
||||||
@@ -118,7 +126,12 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
endwin();
|
endwin();
|
||||||
closeServer();
|
//closeServer();
|
||||||
|
|
||||||
|
if (mode == 1)
|
||||||
|
closeServer();
|
||||||
|
else if (mode == 2)
|
||||||
|
closeClient();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
22
server.cc
22
server.cc
@@ -1,3 +1,4 @@
|
|||||||
|
#include <cstdio>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
@@ -40,13 +41,19 @@ pollForClient()
|
|||||||
{
|
{
|
||||||
char msg[1024];
|
char msg[1024];
|
||||||
while (1) {
|
while (1) {
|
||||||
|
client_message_loop:
|
||||||
// receive a message from the client (listen)
|
// receive a message from the client (listen)
|
||||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||||
bytesRead += recv(clientSocketDescriptor, (char *)&msg, sizeof(msg), 0);
|
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)
|
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||||
linePos++;
|
linePos++;
|
||||||
|
|
||||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +105,7 @@ setupServer(int port)
|
|||||||
aaa->newSockAddrSize = newSockAddrSize;
|
aaa->newSockAddrSize = newSockAddrSize;
|
||||||
int rc = pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
|
int rc = pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
|
||||||
pthread_detach(client_wait_thread);
|
pthread_detach(client_wait_thread);
|
||||||
writeToFile(logFileName, "Server started sucessfully");
|
writeToFile(logFileName, "Server started successfully");
|
||||||
gettimeofday(&start1, NULL);
|
gettimeofday(&start1, NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -116,8 +123,9 @@ closeServer()
|
|||||||
close(serverSocketDescriptor);
|
close(serverSocketDescriptor);
|
||||||
|
|
||||||
/* Don't just die silently */
|
/* Don't just die silently */
|
||||||
printf("********Session********\n");
|
puts("********Session********");
|
||||||
printf("Bytes written: %i\nBytes Read: %i\n", bytesWritten, bytesRead);
|
printf("Bytes written: %i\n", bytesWritten);
|
||||||
printf("Elapsed time: %ld\n secs\n", e);
|
printf("Bytes read: %i\n", bytesRead);
|
||||||
printf("Connection closed...\n");
|
printf("Elapsed time: %ld secs\n", e);
|
||||||
|
puts("Connection closed.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user