Created the second homework, aka "echoserver."
This commit is contained in:
parent
e8882fa175
commit
2ff9635484
4 changed files with 168 additions and 0 deletions
115
echoserver/echoserver.c
Normal file
115
echoserver/echoserver.c
Normal 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: ???
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue