From c7952fa2a149bf15eeb65de3df201e7881c2ada8 Mon Sep 17 00:00:00 2001 From: cdez Date: Wed, 5 Feb 2025 19:15:41 -0700 Subject: [PATCH] completed the first homework, testing complete. --- timeserver/timeserver.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/timeserver/timeserver.c b/timeserver/timeserver.c index e015e64..6eef908 100644 --- a/timeserver/timeserver.c +++ b/timeserver/timeserver.c @@ -44,18 +44,21 @@ int main(int argc, char **argv) // grab the data from the client receivedDataLen = recvfrom(socketTimeFD, message, BUFFER_SIZE, MSG_WAITALL, &clientAddr, &clientAddrLen); message[receivedDataLen] = '\0'; - printf("Message rec: %s", message); // if "time\r\n" is received, then send() back the linux epoch. if(strcmp(message, "time\n") == 0 || strcmp(message, "time\r\n") == 0) { - // + // 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(socketTimeFD, (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)); } else if (strcmp(message, "close\n") == 0 || strcmp(message, "quit\n") == 0 || strcmp(message, "q\n") == 0) { - // + // if the server recv'd the close command, then close the server. printf("\nServer received close.\n"); quitFlag = false; }