aboutsummaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-18 18:23:35 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-18 18:23:35 -0400
commit8a25dcda67683817ddd55b669111f7dd8e3107ef (patch)
treeece530621214be8afdcaf312787cb04e343c563d /client.c
parentbc24590991cb27e8bd220fd6d0585e76f804601d (diff)
decent connection, but not there yet
Diffstat (limited to 'client.c')
-rw-r--r--client.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/client.c b/client.c
index 1acd3ae..6e6c95a 100644
--- a/client.c
+++ b/client.c
@@ -12,6 +12,7 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <ctype.h>
+#include <pthread.h>
#include <arpa/inet.h>
@@ -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