forked from scott/threaded_network_chat
Compare commits
2 Commits
cf41cb45e1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ae315d33a | |||
| dcaa577834 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
client.txt
|
||||||
|
server.txt
|
||||||
|
main
|
||||||
102
main.cpp
102
main.cpp
@@ -1,6 +1,7 @@
|
|||||||
/*an attempt at pthread and network and ncurses all in the same function
|
/*an attempt at pthread and network and ncurses all in the same function
|
||||||
*/
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -15,7 +16,6 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ int displayFile(string path, int startLineNum = 0, int numLines = 10)
|
|||||||
if (num >= startLineNum)
|
if (num >= startLineNum)
|
||||||
{
|
{
|
||||||
move(lineNum, 0);
|
move(lineNum, 0);
|
||||||
printw(line.c_str());
|
printw("%s", line.c_str());
|
||||||
lineNum++;//increment the row number after printing each line
|
lineNum++;//increment the row number after printing each line
|
||||||
}
|
}
|
||||||
num++;
|
num++;
|
||||||
@@ -184,7 +184,7 @@ int setupServer(int port)
|
|||||||
//keeps from bricking the terminal if this happens
|
//keeps from bricking the terminal if this happens
|
||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
cerr << "Error establishing the server socket" << endl;
|
fprintf(stderr, "Error establishing the server socket!\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
//bind the socket to its local address
|
//bind the socket to its local address
|
||||||
@@ -195,7 +195,7 @@ int setupServer(int port)
|
|||||||
//keeps from bricking the terminal if this happens
|
//keeps from bricking the terminal if this happens
|
||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
cerr << "Error binding socket to local address" << endl;
|
fprintf(stderr, "Error binding socket to local address!\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
writeToFile(logFileName, "Waiting for a client to connect...");
|
writeToFile(logFileName, "Waiting for a client to connect...");
|
||||||
@@ -215,59 +215,35 @@ int setupServer(int port)
|
|||||||
writeToFile(logFileName, "Server started sucessfully");
|
writeToFile(logFileName, "Server started sucessfully");
|
||||||
gettimeofday(&start1, NULL);
|
gettimeofday(&start1, NULL);
|
||||||
|
|
||||||
/*while(1)
|
return 0;
|
||||||
{
|
|
||||||
//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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeServer()
|
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
|
//we need to close the socket descriptors after we're all done
|
||||||
gettimeofday(&end1, NULL);
|
gettimeofday(&end1, NULL);
|
||||||
close(clientSocketDescriptor);
|
close(clientSocketDescriptor);
|
||||||
close(serverSocketDescriptor);
|
close(serverSocketDescriptor);
|
||||||
cout << "********Session********" << endl;
|
|
||||||
cout << "Bytes written: " << bytesWritten << " Bytes read: " << bytesRead << endl;
|
/* Don't just die silently */
|
||||||
cout << "Elapsed time: " << (end1.tv_sec - start1.tv_sec)
|
printf("********Session********\n");
|
||||||
<< " secs" << endl;
|
printf("Bytes written: %i\nBytes Read: %i\n", bytesWritten, bytesRead);
|
||||||
cout << "Connection closed..." << endl;
|
printf("Elapsed time: %ld\n secs\n", e);
|
||||||
|
printf("Connection closed...\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeClient()
|
void closeClient()
|
||||||
{
|
{
|
||||||
|
long e = end1.tv_sec - start1.tv_sec;
|
||||||
|
|
||||||
gettimeofday(&end1, NULL);
|
gettimeofday(&end1, NULL);
|
||||||
close(clientSocketDescriptor);
|
close(clientSocketDescriptor);
|
||||||
cout << "********Session********" << endl;
|
printf("********Session********");
|
||||||
cout << "Bytes written: " << bytesWritten <<
|
printf("Bytes written: %i\nBytes read: %i\n", bytesWritten, bytesRead);
|
||||||
" Bytes read: " << bytesRead << endl;
|
printf("Elapsed time: %ld\n secs\n", e);
|
||||||
cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec)
|
printf("Connection closed...\n");
|
||||||
<< " secs" << endl;
|
|
||||||
cout << "Connection closed" << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* pollForSever(void* args)
|
void* pollForSever(void* args)
|
||||||
@@ -277,18 +253,6 @@ void* pollForSever(void* args)
|
|||||||
char msg[1024];
|
char msg[1024];
|
||||||
while(1)
|
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
|
memset(&msg, 0, sizeof(msg));//clear the buffer
|
||||||
bytesRead += recv(socketDescriptor, (char*)&msg, sizeof(msg), 0);
|
bytesRead += recv(socketDescriptor, (char*)&msg, sizeof(msg), 0);
|
||||||
|
|
||||||
@@ -300,6 +264,7 @@ void* pollForSever(void* args)
|
|||||||
break;
|
break;
|
||||||
}*/
|
}*/
|
||||||
//cout << "Server: " << msg << endl;
|
//cout << "Server: " << msg << endl;
|
||||||
|
//printf("Server: %s\n");
|
||||||
writeToFile(logFileName, msg);
|
writeToFile(logFileName, msg);
|
||||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||||
{
|
{
|
||||||
@@ -342,29 +307,6 @@ void setupClient()
|
|||||||
aaa->auxInt = clientSocketDescriptor;
|
aaa->auxInt = clientSocketDescriptor;
|
||||||
int rc = pthread_create(&client_wait_thread, nullptr, pollForSever, aaa);
|
int rc = pthread_create(&client_wait_thread, nullptr, pollForSever, aaa);
|
||||||
pthread_detach(client_wait_thread);
|
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
|
//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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
//doesnt fucking work
|
|
||||||
if (argc > 1 && argv[1][0] == 'c')
|
if (argc > 1 && argv[1][0] == 'c')
|
||||||
{
|
{
|
||||||
//client mode
|
//client mode
|
||||||
@@ -457,4 +398,5 @@ int main(int argc, char *argv[])
|
|||||||
closeServer();
|
closeServer();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user