From 1263cbdbb6cf3ebbb157286b2bb2e488e4b931c8 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Wed, 20 Sep 2023 04:04:22 +0000 Subject: fix handshake protocol --- client.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'client.c') diff --git a/client.c b/client.c index ecb0be9..a989919 100644 --- a/client.c +++ b/client.c @@ -88,15 +88,11 @@ int main(int argc, char *argv[]) freeaddrinfo(servinfo); // all done with this structure - struct Welcome msg; - // recv the message, check for errors too - if ((recvbytes = recv(sockfd, (char*)&msg, sizeof(struct Welcome), 0)) == -1) { - perror("recv"); - exit(1); - } - msg.numStations = ntohs(msg.numStations); - printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); - + pthread_t reply_thread; + pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); + + usleep(1000); + struct Hello hello; hello.commandType = 0; // convert updPort to an int @@ -106,11 +102,6 @@ int main(int argc, char *argv[]) perror("send"); exit(1); } - // print the amount of bytes sent - printf("client: sent %d bytes on a hello call \n", numbytessent); - - pthread_t reply_thread; - pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); char input[LINE_MAX]; printf("Enter a number to change to it's station. Click q to end stream.\n"); @@ -166,6 +157,14 @@ void *reply_thread_routine(void* args) { // print out the fields of reply on one line printf("\nclient: replyType: %d, stringSize: %d\n", reply.replyType, reply.stringSize); + if (reply.replyType == 2) { + struct Welcome msg; + // recv the message, check for errors too + memcpy(&msg, buf, sizeof(struct Welcome)); + msg.numStations = ntohs(msg.numStations); + printf("Welcome to Snowcast! The server has %d stations.\n", msg.numStations); + } + // print the size of reply if (reply.replyType == 3) { printf("client: received an announce message\n"); -- cgit v1.2.3-70-g09d2