Created the second homework, aka "echoserver."

This commit is contained in:
Charles Descamps 2025-02-12 21:30:12 -07:00
parent e8882fa175
commit 2ff9635484
4 changed files with 168 additions and 0 deletions

20
echoserver/Makefile Normal file
View file

@ -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

10
echoserver/README.md Normal file
View file

@ -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.

115
echoserver/echoserver.c Normal file
View file

@ -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: ???
*/

23
echoserver/echoserver.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef ECHOSERVER
#define ECHOSERVER
// included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// data structures
// function prototypes
#endif // ECHOSERVER