From 8a25dcda67683817ddd55b669111f7dd8e3107ef Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 18 Sep 2023 18:23:35 -0400 Subject: decent connection, but not there yet --- client.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'client.c') diff --git a/client.c b/client.c index 1acd3ae..6e6c95a 100644 --- a/client.c +++ b/client.c @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -22,6 +23,7 @@ #define MAX_READ_SIZE 1024 #define LINE_MAX 1024 +void *reply_thread_routine(void *args); // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) @@ -105,6 +107,9 @@ int main(int argc, char *argv[]) exit(1); } + 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"); while (1) { @@ -133,4 +138,41 @@ int main(int argc, char *argv[]) } return 0; +} + +void *reply_thread_routine(void* args) { + int sockfd = (int)args; + int recvbytes; + char buf[MAX_READ_SIZE]; + while (1) { + // recv the message, check for errors too + if ((recvbytes = recv(sockfd, &buf, MAX_READ_SIZE, 0)) == -1) { + perror("recv"); + exit(1); + } + buf[recvbytes] = '\0'; + printf("client: received %d bytes on a reply call \n", recvbytes); + // 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("%d", buf[i]); + } + + struct Reply reply; + memcpy(&reply, buf, sizeof(struct Reply)); + + if (reply.replyType == 3) { + printf("client: received an announce message\n"); + + continue; + } else if (reply.replyType == 4) { + printf("client: received an invalid command message\n"); + + continue; + } + printf("client: received an unknown message\n"); + + memset(buf, 0, MAX_READ_SIZE); + } } \ No newline at end of file -- cgit v1.2.3-70-g09d2