Cleaned up the assignment 2 code, made sure that the debug statements that are less than helpful are removed.

This commit is contained in:
Charles Descamps 2025-02-20 11:20:03 -07:00
parent da93ce1541
commit 44b997c69f

View file

@ -62,8 +62,6 @@ int main(int argc, char **argv)
printf("Echoserver listening on port: %d\n", ECHOPORT); printf("Echoserver listening on port: %d\n", ECHOPORT);
while (quitFlag) while (quitFlag)
{ {
// somehow use the set of file descriptor sockets here
// CODE HERE
// use the select() function here to check the set of sockets // use the select() function here to check the set of sockets
int selectedFD = select(maxFD+1, &socketList, NULL, NULL, NULL); int selectedFD = select(maxFD+1, &socketList, NULL, NULL, NULL);
// check to make sure select() didn't error out. // check to make sure select() didn't error out.
@ -79,7 +77,6 @@ int main(int argc, char **argv)
// check to see if the file descriptor is in the set of sockets using FD_ISSET() // check to see if the file descriptor is in the set of sockets using FD_ISSET()
if(FD_ISSET(fileIter, &socketList)) if(FD_ISSET(fileIter, &socketList))
{ {
printf("\nSocket has been found: %d\n", fileIter);
// if the server socket is being written to/read from, // if the server socket is being written to/read from,
if(fileIter == socketEchoFD) if(fileIter == socketEchoFD)
{ {
@ -104,22 +101,18 @@ int main(int argc, char **argv)
// if it is, set the new max. // if it is, set the new max.
maxFD = clientEchoFD; maxFD = clientEchoFD;
} }
printf("\nConnected client to socket: %d\n", clientEchoFD); printf("\nConnected client to socket.\n");
} }
printf("\nAccepted connection from new client.\n"); printf("\nAccepted connection from new client.\n");
} }
// if the client socket(s) are being written to/read from, // if the client socket(s) are being written to/read from,
else else
{ {
// grab the size of the listening client (not needed?)
// grab the client's socket information (not needed?)
// check to see if the client socket is actually connected (not needed?)
// grab the data from the client, save to message. // grab the data from the client, save to message.
messageBytes = recv(fileIter, message, BUFFER_SIZE, 0); messageBytes = recv(fileIter, message, BUFFER_SIZE, 0);
// make sure that the connection is still open before attempting to send. // make sure that the connection is still open before attempting to send.
if(messageBytes == 0) // old: messageBytes <= 0 if(messageBytes == 0) // old: messageBytes <= 0
{ {
printf("\nmessageBytes: %d\n", messageBytes);
// if it is not open, close the socket and remove the socket from the socket list using FD_CLR() // if it is not open, close the socket and remove the socket from the socket list using FD_CLR()
close(fileIter); close(fileIter);
FD_CLR(fileIter, &socketList); FD_CLR(fileIter, &socketList);
@ -131,44 +124,9 @@ int main(int argc, char **argv)
send(fileIter, message, messageBytes, 0); send(fileIter, message, messageBytes, 0);
printf("\nMessage has been sent back to client.\n"); printf("\nMessage has been sent back to client.\n");
} }
printf("\nMessage received.\n");
} }
} }
} }
// OLD CODE HERE
// // 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);
// // otherwise, repeat/echo back the message to the client.
// // 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);
// END OLD CODE HERE
} }
// close the socket // close the socket
close(socketEchoFD); close(socketEchoFD);