transfer to other machine

This commit is contained in:
2025-07-27 14:08:08 -06:00
parent b7b1688e88
commit 39d28a54c1
+36 -28
View File
@@ -26,30 +26,27 @@ pthread_mutex_t sendmut, receivemut;
int sockfd, confd; int sockfd, confd;
void *input(void *arg) { void *input(void *arg) {
char *msg; char msg[128];
char *copy;
while (1) { while (1) {
msg = malloc(30); memset(msg, 0, 128);
printf("Type a message: "); printf("Type a message: ");
fgets(msg, sizeof(msg), stdin); fgets(msg, sizeof(msg), stdin);
copy = malloc(30);
strcpy(copy, msg);
pthread_mutex_lock(&sendmut); pthread_mutex_lock(&sendmut);
ListAppend(sendlist, copy); ListAppend(sendlist, msg);
pthread_mutex_unlock(&sendmut); pthread_mutex_unlock(&sendmut);
} }
return 0; return 0;
} }
void *sender(void *arg) { void *sender(void *arg) {
char *msg; char msg[128];
while (1) { while (1) {
msg = malloc(30); memset(msg, 0, 128);
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); send(confd, msg, sizeof(msg), 0);
ListRemove(sendlist); ListRemove(sendlist);
} }
else { else {
@@ -57,33 +54,47 @@ void *sender(void *arg) {
continue; continue;
} }
pthread_mutex_unlock(&sendmut); 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; return 0;
} }
void *receiver(void *arg) { void *receiver(void *arg) {
char msg[30]; char buf[128];
char *copy = malloc(31); int bytes;
while (1) { while (1) {
recv(sockfd, msg, 30, 0); memset(buf, 0, 128);
if (msg[0] != '\n' && msg[0] != '\0') { bytes = recv(sockfd, buf, 128, 0);
pthread_mutex_lock(&receivemut); /*pthread_mutex_lock(&receivemut);
ListAppend(receivelist, msg); ListAppend(receivelist, buf);
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
*/
if (bytes != -1) {
printf("Received: %s\n", buf);
}
else {
perror("recv");
} }
} }
return 0; return 0;
} }
void *output(void *arg) { void *output(void *arg) {
char *msg; char msg[128];
while (1) { while (1) {
memset(msg, 0, 128);
pthread_mutex_lock(&receivemut); pthread_mutex_lock(&receivemut);
if (ListCount(receivelist) != 0) { if (ListCount(receivelist) != 0) {
ListFirst(receivelist); ListFirst(receivelist);
msg = ListCurr(receivelist); strcpy(msg, ListCurr(receivelist));
ListRemove(receivelist); ListRemove(receivelist);
printf("%s\n", (char*)msg); printf("%s\n", msg);
} }
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
} }
@@ -190,7 +201,6 @@ 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));
@@ -211,7 +221,7 @@ int main(int argc, char* argv[]) {
} }
inet_ntop(q->ai_family, get_in_addr((struct sockaddr *)q->ai_addr), inet_ntop(q->ai_family, get_in_addr((struct sockaddr *)q->ai_addr),
s, sizeof s); 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) { if (connect(sockfd, q->ai_addr, q->ai_addrlen) == -1) {
perror("Client: connect"); perror("Client: connect");
@@ -228,20 +238,18 @@ int main(int argc, char* argv[]) {
else { else {
servinfo = q; 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(&send_thread, NULL, sender, NULL);
pthread_create(&receive_thread, NULL, receiver, NULL); pthread_create(&receive_thread, NULL, receiver, NULL);
/*pthread_create(&input_thread, NULL, input, NULL);
pthread_create(&output_thread, NULL, output, NULL); pthread_create(&output_thread, NULL, output, NULL);
pthread_join(input_thread, NULL); pthread_join(input_thread, NULL);
pthread_join(output_thread, NULL);
*/
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);
*/
close(sockfd); close(sockfd);
close(confd); close(confd);