send test

This commit is contained in:
2025-07-26 18:15:14 -06:00
parent 6086c8ec2c
commit b7b1688e88
+17 -7
View File
@@ -26,11 +26,13 @@ pthread_mutex_t sendmut, receivemut;
int sockfd, confd; int sockfd, confd;
void *input(void *arg) { void *input(void *arg) {
char msg[30]; char *msg;
char *copy = malloc(31); char *copy;
while (1) { while (1) {
msg = malloc(30);
printf("Type a message: "); printf("Type a message: ");
fgets(msg, sizeof(msg), stdin); fgets(msg, sizeof(msg), stdin);
copy = malloc(30);
strcpy(copy, msg); strcpy(copy, msg);
pthread_mutex_lock(&sendmut); pthread_mutex_lock(&sendmut);
ListAppend(sendlist, copy); ListAppend(sendlist, copy);
@@ -40,12 +42,14 @@ void *input(void *arg) {
} }
void *sender(void *arg) { void *sender(void *arg) {
char msg[30]; char *msg;
while (1) { while (1) {
msg = malloc(30);
pthread_mutex_lock(&sendmut); pthread_mutex_lock(&sendmut);
if (ListCount(sendlist) != 0) { if (ListCount(sendlist) != 0) {
ListFirst(sendlist); ListFirst(sendlist);
strcpy(msg, ListCurr(sendlist)); strcpy(msg, ListCurr(sendlist));
send(confd, &msg, 30, 0);
ListRemove(sendlist); ListRemove(sendlist);
} }
else { else {
@@ -106,7 +110,8 @@ int main(int argc, char* argv[]) {
char *remote_machine, *local_port, *remote_port; char *remote_machine, *local_port, *remote_port;
int status; int status;
int yes = 1; int yes = 1;
char buf[20];
if (argc != 4) { if (argc != 4) {
printf("Wrong number of arguments. Usage: ./chatty <local port> \ printf("Wrong number of arguments. Usage: ./chatty <local port> \
<remote IP> <remote port>\n"); <remote IP> <remote port>\n");
@@ -185,7 +190,8 @@ int main(int argc, char* argv[]) {
s, s,
sizeof s); sizeof s);
printf("Getting chatty with %s\n", s); printf("Getting chatty with %s\n", s);
} send(confd, "test", 4, 0);
}
else { else {
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
@@ -222,9 +228,11 @@ int main(int argc, char* argv[]) {
else { else {
servinfo = q; 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(&send_thread, NULL, sender, NULL);
pthread_create(&receive_thread, NULL, receiver, NULL); pthread_create(&receive_thread, NULL, receiver, NULL);
pthread_create(&output_thread, NULL, output, 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(send_thread, NULL);
pthread_join(receive_thread, NULL); pthread_join(receive_thread, NULL);
pthread_join(output_thread, NULL); pthread_join(output_thread, NULL);
*/
close(sockfd); close(sockfd);
close(confd); close(confd);
return 0; return 0;