diff --git a/echoserver/Makefile b/echoserver/Makefile new file mode 100644 index 0000000..221b29f --- /dev/null +++ b/echoserver/Makefile @@ -0,0 +1,20 @@ +CC = gcc -Wall -std=c11 -pedantic +SOURCES = echoserver.c +OBJECTS = echoserver.o + +all: echoserver + +echoserver: $(OBJECTS) + $(CC) $(OBJECTS) -o echoserver + +echoserver.o: echoserver.c + $(CC) -c echoserver.c -o echoserver.o + +clean: + rm -f $(OBJECTS) echoserver echoserver.out + +clean_objects: + rm -f $(OBJECTS) + +run: + ./echoserver diff --git a/echoserver/README.md b/echoserver/README.md new file mode 100644 index 0000000..fb81333 --- /dev/null +++ b/echoserver/README.md @@ -0,0 +1,10 @@ +# Running the echoserver +Use `make` to compile the echoserver. +Use `make run` to run the echoserver. This will run on an infinite loop. +Use `Ctrl+C` to stop the echoserver. + +## echoserver communication +Communicating with the server can happen using telnet in TCP mode. + +## echoserver information +The echoserver runs on TCP port 8888. \ No newline at end of file diff --git a/echoserver/echoserver.c b/echoserver/echoserver.c new file mode 100644 index 0000000..f41f986 --- /dev/null +++ b/echoserver/echoserver.c @@ -0,0 +1,115 @@ +// header files +#include "echoserver.h" + +// constants +// echoserver port is 8888 +#define ECHOPORT 8888 +#define BUFFER_SIZE 1024 +#define MAX_CONNECT_QUEUE 10 + +// prototypes + +// main function +int main(int argc, char **argv) +{ + // initialize program (variables and structs) + bool quitFlag = true; + int socketEchoFD; + int clientEchoFD; + char message[BUFFER_SIZE]; + struct sockaddr_in serverAddr; + struct sockaddr_storage clientAddr; + socklen_t clientSize; + memset(&serverAddr, 0, sizeof(serverAddr)); + memset(&clientAddr, 0, sizeof(clientAddr)); + // define a TCP socket + socketEchoFD = socket(AF_INET, SOCK_STREAM, 0); + if(socketEchoFD < 0) + { + printf("Unable to create the socket.\n"); + return -1; + } + // use the setsockopt function to make the socket accept different configurations + // CODE HERE + // bind the TCP socket to port 8888 / ECHOPORT + serverAddr.sin_family = AF_INET; + serverAddr.sin_addr.s_addr = INADDR_ANY; + serverAddr.sin_port = htons(ECHOPORT); + int bindCheck = bind(socketEchoFD, &serverAddr, sizeof(serverAddr)); + if(bindCheck < 0) + { + printf("Unable to bind the socket to the port or address.\n Is either in use?"); + return -1; + } + // the server will now accept client connections by listening. + listen(socketEchoFD, MAX_CONNECT_QUEUE); + // while loop that will continue to listen for messages from clients. + printf("Echoserver listening on port: %d\n", ECHOPORT); + // while(quitFlag) + // { + // int clientAddrLen; + // int receivedDataLen; + // // grab the data from the client + // receivedDataLen = recvfrom(socketEchoFD, message, BUFFER_SIZE, MSG_WAITALL, &clientAddr, &clientAddrLen); + // message[receivedDataLen] = '\0'; + // // if "01189998811991197253\r\n" is received, then close the server. + // if(strcmp(message, "01189998811991197253\n") == 0 || strcmp(message, "01189998811991197253\r\n") == 0) + // { + // // if the server recv'd the close command, then close the server. + // printf("\nServer received close.\n"); + // quitFlag = false; + // } + // // otherwise, repeat/echo back the message to the client. + // // setup the reply message + // char reply[BUFFER_SIZE]; + // // add the current time to the reply + // sprintf(reply, "%d", time(NULL)); + // // send the reply to the requestee + // sendto(socketEchoFD, (const char *)reply, strlen(reply), 0, (const struct sockaddr *)&clientAddr, clientAddrLen); + // // log the time command recv'd + // printf("\nServer received time. Replied with: %d\n", time(NULL)); + // } + while (quitFlag) + { + // grab the size of the listened clients + clientSize = sizeof(clientAddr); + // grab the client's socket information + clientEchoFD = accept(socketEchoFD, &clientAddr, &clientSize); + // check to see if the client socket is actually connected + if(clientEchoFD == -1) + { + printf("Unable to accept from the client.\n"); + return -1; + } + // grab the data from the client, save to message. + // NEW CODE HERE + // if "01189998811991197253\r\n" is received, then close the server. + if (strcmp(message, "01189998811991197253\n") == 0 || strcmp(message, "01189998811991197253\r\n") == 0) + { + // if the server recv'd the close command, then close the server. + printf("\nServer received close.\n"); + quitFlag = false; + } + // otherwise, repeat/echo back the message to the client. + // NEW CODE HERE + // close the accepted listening socket since it is no longer needed. + close(clientEchoFD); + } + // close the socket + close(socketEchoFD); + // profit??? + return 0; +} + +// function definitions + +/* +Name: ??? +Process: ??? +Function Input/Parameters: ??? +Function Output/Parameters: ??? +Function Output/Returned: ??? +Device Input/Device: ??? +Device Output/Device: ??? +Dependencies: ??? +*/ \ No newline at end of file diff --git a/echoserver/echoserver.h b/echoserver/echoserver.h new file mode 100644 index 0000000..5a19882 --- /dev/null +++ b/echoserver/echoserver.h @@ -0,0 +1,23 @@ +#ifndef ECHOSERVER +#define ECHOSERVER + +// included libraries +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// data structures + +// function prototypes + +#endif // ECHOSERVER \ No newline at end of file