From 4311db5d742f03080dfa58aeba45dadc175c625c Mon Sep 17 00:00:00 2001 From: sotech117 Date: Wed, 20 Sep 2023 01:55:59 -0400 Subject: more small things to fit assignment spec --- client.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'client.c') diff --git a/client.c b/client.c index df5ea9b..735330c 100644 --- a/client.c +++ b/client.c @@ -117,7 +117,7 @@ int main(int argc, char *argv[]) } else { // convert input to an int int inputInt = atoi(input); - printf("Changing to station %d.\n", inputInt); + // printf("Changing to station %d.\n", inputInt); // send the command to change the station struct SetStation setStation; @@ -139,6 +139,7 @@ void *reply_thread_routine(void* args) { char buf[MAX_READ_SIZE]; while (1) { // recv the message, check for errors too + memset(buf, 0, MAX_READ_SIZE); if ((recvbytes = recv(sockfd, &buf, MAX_READ_SIZE, 0)) == -1) { perror("recv"); exit(1); @@ -148,9 +149,9 @@ void *reply_thread_routine(void* args) { // print the two first field of the call // printf("client: replyType: %d, stringSize: %d\n", buf[0], buf[1]); // print the while buffer by char - for (int i = 0; i < recvbytes; i++) { - printf("%c ", buf[i]); - } + // for (int i = 0; i < recvbytes; i++) { + // printf("%c ", buf[i]); + // } struct Reply reply; memcpy(&reply, buf, 2); @@ -163,6 +164,7 @@ void *reply_thread_routine(void* args) { memcpy(&msg, buf, sizeof(struct Welcome)); msg.numStations = ntohs(msg.numStations); printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); + continue; } // print the size of reply @@ -172,19 +174,21 @@ void *reply_thread_routine(void* args) { char *song_name = malloc(reply.stringSize); // printf(sizeof(struct Reply)); memcpy(song_name, buf + 2, reply.stringSize); - printf("New song announced: %s\n", song_name); - free(song_name); - continue; } else if (reply.replyType == 4) { - printf("client: received an invalid command message\n"); - - continue; + // print sockfd + char *message = malloc(reply.stringSize); + // printf(sizeof(struct Reply)); + memcpy(message, buf + 2, reply.stringSize); + printf("Exiting. %s\n", message); + close(sockfd); + exit(1); } - printf("client: received an unknown message\n"); - memset(buf, 0, MAX_READ_SIZE); + printf("Exiting. Lost connection to server."); + close(sockfd); + exit(1); } } \ No newline at end of file -- cgit v1.2.3-70-g09d2