From b7b1688e88700288ef6444897098273051fcebe2 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Sat, 26 Jul 2025 18:15:14 -0600 Subject: [PATCH 01/10] send test --- main.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 1752b83..5cea86d 100644 --- a/main.c +++ b/main.c @@ -26,11 +26,13 @@ pthread_mutex_t sendmut, receivemut; int sockfd, confd; void *input(void *arg) { - char msg[30]; - char *copy = malloc(31); + char *msg; + char *copy; while (1) { + msg = malloc(30); printf("Type a message: "); fgets(msg, sizeof(msg), stdin); + copy = malloc(30); strcpy(copy, msg); pthread_mutex_lock(&sendmut); ListAppend(sendlist, copy); @@ -40,12 +42,14 @@ void *input(void *arg) { } void *sender(void *arg) { - char msg[30]; + char *msg; while (1) { + msg = malloc(30); pthread_mutex_lock(&sendmut); if (ListCount(sendlist) != 0) { ListFirst(sendlist); strcpy(msg, ListCurr(sendlist)); + send(confd, &msg, 30, 0); ListRemove(sendlist); } else { @@ -106,7 +110,8 @@ int main(int argc, char* argv[]) { char *remote_machine, *local_port, *remote_port; int status; int yes = 1; - + char buf[20]; + if (argc != 4) { printf("Wrong number of arguments. Usage: ./chatty \ \n"); @@ -185,7 +190,8 @@ int main(int argc, char* argv[]) { s, sizeof s); printf("Getting chatty with %s\n", s); - } + send(confd, "test", 4, 0); + } else { memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; @@ -222,9 +228,11 @@ int main(int argc, char* argv[]) { else { servinfo = q; } - printf("Connected!\n"); + recv(sockfd, buf, 4, 0); + printf("%s\n", buf); } - pthread_create(&input_thread, NULL, input, NULL); + /* + pthread_create(&input_thread, NULL, input, NULL); pthread_create(&send_thread, NULL, sender, NULL); pthread_create(&receive_thread, NULL, receiver, NULL); pthread_create(&output_thread, NULL, output, NULL); @@ -232,7 +240,9 @@ int main(int argc, char* argv[]) { pthread_join(send_thread, NULL); pthread_join(receive_thread, NULL); pthread_join(output_thread, NULL); + */ + close(sockfd); close(confd); return 0; From 39d28a54c1c7260715bd09bdaf7e4990d15d0326 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Sun, 27 Jul 2025 14:08:08 -0600 Subject: [PATCH 02/10] transfer to other machine --- main.c | 88 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/main.c b/main.c index 5cea86d..f82bb50 100644 --- a/main.c +++ b/main.c @@ -26,64 +26,75 @@ pthread_mutex_t sendmut, receivemut; int sockfd, confd; void *input(void *arg) { - char *msg; - char *copy; + char msg[128]; while (1) { - msg = malloc(30); + memset(msg, 0, 128); printf("Type a message: "); fgets(msg, sizeof(msg), stdin); - copy = malloc(30); - strcpy(copy, msg); pthread_mutex_lock(&sendmut); - ListAppend(sendlist, copy); + ListAppend(sendlist, msg); pthread_mutex_unlock(&sendmut); } return 0; } void *sender(void *arg) { - char *msg; + char msg[128]; while (1) { - msg = malloc(30); - pthread_mutex_lock(&sendmut); - if (ListCount(sendlist) != 0) { - ListFirst(sendlist); - strcpy(msg, ListCurr(sendlist)); - send(confd, &msg, 30, 0); - ListRemove(sendlist); - } - else { - pthread_mutex_unlock(&sendmut); - continue; - } - pthread_mutex_unlock(&sendmut); + memset(msg, 0, 128); + /*pthread_mutex_lock(&sendmut); + if (ListCount(sendlist) != 0) { + ListFirst(sendlist); + strcpy(msg, ListCurr(sendlist)); + send(confd, msg, sizeof(msg), 0); + ListRemove(sendlist); + } + else { + pthread_mutex_unlock(&sendmut); + continue; + } + pthread_mutex_unlock(&sendmut); + }*/ + printf("Type a message: "); + fgets(msg, sizeof(msg), stdin); + pthread_mutex_lock(&sendmut); + printf("Msg to send: %s\n", msg); + pthread_mutex_unlock(&sendmut); + send(confd, msg, sizeof(msg), 0); } return 0; } void *receiver(void *arg) { - char msg[30]; - char *copy = malloc(31); + char buf[128]; + int bytes; while (1) { - recv(sockfd, msg, 30, 0); - if (msg[0] != '\n' && msg[0] != '\0') { - pthread_mutex_lock(&receivemut); - ListAppend(receivelist, msg); - pthread_mutex_unlock(&receivemut); + memset(buf, 0, 128); + bytes = recv(sockfd, buf, 128, 0); + /*pthread_mutex_lock(&receivemut); + ListAppend(receivelist, buf); + pthread_mutex_unlock(&receivemut); + */ + if (bytes != -1) { + printf("Received: %s\n", buf); + } + else { + perror("recv"); } } return 0; } void *output(void *arg) { - char *msg; + char msg[128]; while (1) { + memset(msg, 0, 128); pthread_mutex_lock(&receivemut); if (ListCount(receivelist) != 0) { ListFirst(receivelist); - msg = ListCurr(receivelist); + strcpy(msg, ListCurr(receivelist)); ListRemove(receivelist); - printf("%s\n", (char*)msg); + printf("%s\n", msg); } pthread_mutex_unlock(&receivemut); } @@ -190,7 +201,6 @@ int main(int argc, char* argv[]) { s, sizeof s); printf("Getting chatty with %s\n", s); - send(confd, "test", 4, 0); } else { memset(&hints, 0, sizeof(hints)); @@ -211,7 +221,7 @@ int main(int argc, char* argv[]) { } inet_ntop(q->ai_family, get_in_addr((struct sockaddr *)q->ai_addr), s, sizeof s); - printf("Client: attempting connection to %s\n", s); + printf("Connecting to %s\n", s); if (connect(sockfd, q->ai_addr, q->ai_addrlen) == -1) { perror("Client: connect"); @@ -228,21 +238,19 @@ int main(int argc, char* argv[]) { else { servinfo = q; } - recv(sockfd, buf, 4, 0); - printf("%s\n", buf); } - /* - pthread_create(&input_thread, NULL, input, NULL); pthread_create(&send_thread, NULL, sender, NULL); pthread_create(&receive_thread, NULL, receiver, NULL); - pthread_create(&output_thread, NULL, output, NULL); + /*pthread_create(&input_thread, NULL, input, NULL); + pthread_create(&output_thread, NULL, output, NULL); pthread_join(input_thread, NULL); - pthread_join(send_thread, NULL); - pthread_join(receive_thread, NULL); pthread_join(output_thread, NULL); - */ + */ + pthread_join(send_thread, NULL); + pthread_join(receive_thread, NULL); + close(sockfd); close(confd); return 0; From 84ac4579461fcd09a794be81e144a848cad9c92b Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Sun, 27 Jul 2025 17:09:45 -0600 Subject: [PATCH 03/10] merge fix --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 5cea86d..88c8c0d 100644 --- a/main.c +++ b/main.c @@ -228,7 +228,7 @@ int main(int argc, char* argv[]) { else { servinfo = q; } - recv(sockfd, buf, 4, 0); + recv(sockfd, buf, 20, 0); printf("%s\n", buf); } /* From 8df5e58f9a59b430a3dea3d577626f49e3b5acaa Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Mon, 28 Jul 2025 00:19:21 -0600 Subject: [PATCH 04/10] fix free issue in ListRemove --- list_removers.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/list_removers.c b/list_removers.c index 3a18fb6..27af6ae 100644 --- a/list_removers.c +++ b/list_removers.c @@ -30,12 +30,9 @@ int ListRemove(LIST *list) { list->current->next->previous = list->current->previous; list->current = list->current->next; } - + unused->data = NULL; free(unused->data); - unused->next = NULL; - unused->previous = NULL; - unused = NULL; free(unused); list->size--; return 0; From d297356eb398ceb3d2edff4f234c86ea5b9a647c Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Mon, 28 Jul 2025 00:43:22 -0600 Subject: [PATCH 05/10] got sending working but only one way? --- main.c | 48 ++++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/main.c b/main.c index 4234cbb..3209bc4 100644 --- a/main.c +++ b/main.c @@ -86,36 +86,21 @@ void *sender(void *arg) { void *receiver(void *arg) { char buf[128]; int bytes; - if (client) { - while (1) { - pthread_mutex_lock(&receivemut); - memset(buf, 0, 128); - bytes = recv(newfd, buf, 128, 0); - pthread_mutex_unlock(&receivemut); - - if (bytes != -1) { - printf("Received: %s\n", buf); - } - else { - perror("recv"); - } + while (1) { + pthread_mutex_lock(&receivemut); + memset(buf, 0, 128); + bytes = recv(confd, buf, 128, 0); + pthread_mutex_unlock(&receivemut); + + if (bytes != -1) { + printf("Received: %s\n", buf); } - } - else { - while (1) { - pthread_mutex_lock(&receivemut); - memset(buf, 0, 128); - bytes = recv(sockfd, buf, 128, 0); - pthread_mutex_unlock(&receivemut); - - if (bytes != -1) { - printf("Received: %s\n", buf); - } - else { - perror("recv"); - } + else { + perror("recv"); + return -1; } } + return 0; } @@ -230,7 +215,7 @@ int main(int argc, char* argv[]) { exit(1); } for (q = servinfo; q != NULL; q = q->ai_next) { - if ((newfd = socket(q->ai_family, + if ((confd = socket(q->ai_family, q->ai_socktype, q->ai_protocol)) == -1) { perror("Client: socket"); @@ -240,7 +225,7 @@ int main(int argc, char* argv[]) { s, sizeof s); printf("Connecting to %s\n", s); - if (connect(newfd, q->ai_addr, q->ai_addrlen) == -1) { + if (connect(confd, q->ai_addr, q->ai_addrlen) == -1) { perror("Client: connect"); close(sockfd); continue; @@ -252,9 +237,8 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Client: failed to create socket\n"); return 2; } - else { - servinfo = q; - } + servinfo = q; + } pthread_create(&send_thread, NULL, sender, NULL); pthread_create(&receive_thread, NULL, receiver, NULL); From 207c7a2919a6174c637ea210489cc86cf86fd857 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Mon, 28 Jul 2025 00:48:22 -0600 Subject: [PATCH 06/10] got two way communication --- main.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 3209bc4..7ad01c8 100644 --- a/main.c +++ b/main.c @@ -60,25 +60,11 @@ void *output(void *arg) { void *sender(void *arg) { char msg[128]; - if (client) { - while (1) { - pthread_mutex_lock(&sendmut); - memset(msg, 0, 128); - printf("Type a message: "); - fgets(msg, sizeof(msg), stdin); - send(newfd, msg, sizeof(msg), 0); - pthread_mutex_unlock(&sendmut); - } - } - else { - while (1) { - pthread_mutex_lock(&sendmut); - memset(msg, 0, 128); - printf("Type a message: "); - fgets(msg, sizeof(msg), stdin); - send(confd, msg, sizeof(msg), 0); - pthread_mutex_unlock(&sendmut); - } + while (1) { + memset(msg, 0, 128); + printf("Type a message: "); + fgets(msg, sizeof(msg), stdin); + send(confd, msg, sizeof(msg), 0); } return 0; } @@ -87,10 +73,8 @@ void *receiver(void *arg) { char buf[128]; int bytes; while (1) { - pthread_mutex_lock(&receivemut); memset(buf, 0, 128); bytes = recv(confd, buf, 128, 0); - pthread_mutex_unlock(&receivemut); if (bytes != -1) { printf("Received: %s\n", buf); From 49bdf4f568512a54af132d0c86c0e566226a91fa Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Mon, 28 Jul 2025 13:00:19 -0600 Subject: [PATCH 07/10] fix return value --- main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 7ad01c8..05b3d20 100644 --- a/main.c +++ b/main.c @@ -72,6 +72,7 @@ void *sender(void *arg) { void *receiver(void *arg) { char buf[128]; int bytes; + char error[] = "pthread exit"; while (1) { memset(buf, 0, 128); bytes = recv(confd, buf, 128, 0); @@ -81,7 +82,7 @@ void *receiver(void *arg) { } else { perror("recv"); - return -1; + pthread_exit(&error); } } From 6a9f876ccd2ddaa58b377cdf798db4de19893188 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Tue, 29 Jul 2025 00:45:06 -0600 Subject: [PATCH 08/10] better error checking --- main.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 05b3d20..c40b806 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,7 @@ LIST *receivelist, *sendlist; pthread_mutex_t sendmut, receivemut; -int sockfd, confd, newfd, client; +int sockfd, confd, client; char *remote_machine, *local_port, *remote_port; /* void *input(void *arg) { @@ -60,11 +60,18 @@ void *output(void *arg) { void *sender(void *arg) { char msg[128]; + int bytes; + char error[] = "pthread sender exit"; + while (1) { memset(msg, 0, 128); printf("Type a message: "); fgets(msg, sizeof(msg), stdin); - send(confd, msg, sizeof(msg), 0); + bytes = send(confd, msg, sizeof(msg), 0); + if (bytes == -1) { + perror("send"); + pthread_exit(&error); + } } return 0; } @@ -72,13 +79,16 @@ void *sender(void *arg) { void *receiver(void *arg) { char buf[128]; int bytes; - char error[] = "pthread exit"; + char error[] = "pthread receiver exit"; while (1) { memset(buf, 0, 128); bytes = recv(confd, buf, 128, 0); if (bytes != -1) { - printf("Received: %s\n", buf); + printf("\nReceived: %s\n", buf); + } + else if (bytes == 0) { + pthread_exit("connection closed"); } else { perror("recv"); From 2fd3f1c81ff564e2d1c421f563fd3e886ff57ee2 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Tue, 5 Aug 2025 14:08:22 -0600 Subject: [PATCH 09/10] initial ncurses stuff --- main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index c40b806..ceb0506 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ #include #include #include +#include #define MAXBUFSIZE 30 #define BACKLOG 10 @@ -25,6 +26,7 @@ LIST *receivelist, *sendlist; pthread_mutex_t sendmut, receivemut; int sockfd, confd, client; char *remote_machine, *local_port, *remote_port; +int row, col; /* void *input(void *arg) { char msg[128]; @@ -65,8 +67,9 @@ void *sender(void *arg) { while (1) { memset(msg, 0, 128); - printf("Type a message: "); - fgets(msg, sizeof(msg), stdin); + mvprintw(row - 2, 0, "Type a message: "); + refresh(); + getstr(msg); bytes = send(confd, msg, sizeof(msg), 0); if (bytes == -1) { perror("send"); @@ -235,6 +238,8 @@ int main(int argc, char* argv[]) { servinfo = q; } + initscr(); + getmaxyx(stdscr, row, col); pthread_create(&send_thread, NULL, sender, NULL); pthread_create(&receive_thread, NULL, receiver, NULL); /*pthread_create(&input_thread, NULL, input, NULL); @@ -249,6 +254,7 @@ int main(int argc, char* argv[]) { close(sockfd); close(confd); + endwin(); return 0; } From f33a2a6dbd5dd5c6a2943260c0e7f4e0cde21843 Mon Sep 17 00:00:00 2001 From: Dane MacFadden Date: Tue, 5 Aug 2025 14:31:01 -0600 Subject: [PATCH 10/10] update Makefile and fix minor bugs --- Makefile | 2 +- main.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0d8259b..460025d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS = -g CPPFLAGS = -Wall -Wextra -pedantic -std=gnu90 chatty: main.o liblist.a - $(CC) $(CFLAGS) -L. -o chatty main.o -llist -lpthread + $(CC) $(CFLAGS) -L. -o chatty main.o -llist -lpthread -lncurses main.o: main.c $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o main.o main.c diff --git a/main.c b/main.c index ceb0506..e32616a 100644 --- a/main.c +++ b/main.c @@ -88,7 +88,8 @@ void *receiver(void *arg) { bytes = recv(confd, buf, 128, 0); if (bytes != -1) { - printf("\nReceived: %s\n", buf); + mvprintw(row - 3, 10, ("Message: %s", buf)); + refresh(); } else if (bytes == 0) { pthread_exit("connection closed");