forked from scott/threaded_network_chat
initial commit
This commit is contained in:
123
server.cc
Normal file
123
server.cc
Normal file
@@ -0,0 +1,123 @@
|
||||
#include <curses.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "disp.h"
|
||||
#include "log.h"
|
||||
#include "public.h"
|
||||
#include "server.h"
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
void *
|
||||
waitForClient(void *argss)
|
||||
{
|
||||
waitClientArgs *args = static_cast<waitClientArgs *>(argss);
|
||||
clientSocketDescriptor =
|
||||
accept(serverSocketDescriptor, (sockaddr *)&args->newSockAddr, &args->newSockAddrSize);
|
||||
if (clientSocketDescriptor >= 0) {
|
||||
writeToFile(logFileName, "client connected");
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
}
|
||||
delete args; // delete to avoid memory leaks
|
||||
|
||||
return pollForClient();
|
||||
}
|
||||
|
||||
void *
|
||||
pollForClient()
|
||||
{
|
||||
char msg[1024];
|
||||
while (1) {
|
||||
// receive a message from the client (listen)
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead += recv(clientSocketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
writeToFile(logFileName,
|
||||
msg); // write the client's message to file
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// set up a suitable server for this
|
||||
int
|
||||
setupServer(int port)
|
||||
{
|
||||
// setup a socket and connection tools
|
||||
sockaddr_in servAddr;
|
||||
bzero((char *)&servAddr, sizeof(servAddr));
|
||||
servAddr.sin_family = AF_INET;
|
||||
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
servAddr.sin_port = htons(port);
|
||||
|
||||
// open stream oriented socket with internet address
|
||||
// also keep track of the socket descriptor
|
||||
serverSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverSocketDescriptor < 0) {
|
||||
// keeps from bricking the terminal if this happens
|
||||
endwin();
|
||||
|
||||
fprintf(stderr, "Error establishing the server socket!\n");
|
||||
exit(0);
|
||||
}
|
||||
// bind the socket to its local address
|
||||
int bindStatus =
|
||||
bind(serverSocketDescriptor, (struct sockaddr *)&servAddr, sizeof(servAddr));
|
||||
if (bindStatus < 0) {
|
||||
// keeps from bricking the terminal if this happens
|
||||
endwin();
|
||||
|
||||
fprintf(stderr, "Error binding socket to local address!\n");
|
||||
exit(0);
|
||||
}
|
||||
writeToFile(logFileName, "Waiting for a client to connect...");
|
||||
// listen for up to 5 requests at a time
|
||||
listen(serverSocketDescriptor, 5);
|
||||
// receive a request from client using accept
|
||||
// we need a new address to connect with the client
|
||||
sockaddr_in newSockAddr;
|
||||
socklen_t newSockAddrSize = sizeof(newSockAddr);
|
||||
// accept, create a new socket descriptor to
|
||||
// handle the new connection with client
|
||||
auto *aaa = new waitClientArgs{}; // it looks stupid but it works
|
||||
aaa->newSockAddr = newSockAddr;
|
||||
aaa->newSockAddrSize = newSockAddrSize;
|
||||
int rc = pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
|
||||
pthread_detach(client_wait_thread);
|
||||
writeToFile(logFileName, "Server started sucessfully");
|
||||
gettimeofday(&start1, NULL);
|
||||
|
||||
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);
|
||||
|
||||
/* 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");
|
||||
}
|
||||
Reference in New Issue
Block a user