// 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; 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 non-blocking using setsockopt? // 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. messageBytes = recv(clientEchoFD, message, BUFFER_SIZE, MSG_WAITALL); // 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) { // 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, messageBytes, 0); } // 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: ??? */