CS460-Projects/echoserver/echoserver.c

139 lines
No EOL
5 KiB
C

// 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;
int reuseAddrOpt = 1;
int keepAliveOpt = 1;
char message[BUFFER_SIZE];
int messageBytes;
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
// allow for the reuse of the address/bind.
setsockopt(socketEchoFD, SOL_SOCKET, SO_REUSEADDR, &reuseAddrOpt, sizeof(reuseAddrOpt));
// also need to set the socket to keepalive using setsockopt
setsockopt(socketEchoFD, SOL_SOCKET, SO_KEEPALIVE, &keepAliveOpt, sizeof(keepAliveOpt));
// 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("\nUnable to accept from the client.\n");
return -1;
}
else
{
printf("\nConnection received from client.\n");
}
// grab the data from the client, save to message.
messageBytes = recv(clientEchoFD, message, BUFFER_SIZE, 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.
// NEW CODE HERE
// make sure that the connection is still open before attempting to send.
if(messageBytes <= 0)
{
printf("\nERROR\n");
// this means that the connection is closed, or had another error.
return -1;
}
else
{
// send back the message over the clientEcho socket.
send(clientEchoFD, message, sizeof(message), 0);
printf("\nMessage has been sent back to client.\n");
}
// 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: ???
*/