sync desktop

This commit is contained in:
Dane MacFadden
2025-07-28 00:17:17 -06:00
+82 -57
View File
@@ -23,71 +23,99 @@
LIST *receivelist, *sendlist; LIST *receivelist, *sendlist;
pthread_mutex_t sendmut, receivemut; pthread_mutex_t sendmut, receivemut;
int sockfd, confd; int sockfd, confd, newfd, client;
char *remote_machine, *local_port, *remote_port;
/*
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 *output(void *arg) {
char msg[128];
while (1) {
memset(msg, 0, 128);
pthread_mutex_lock(&receivemut);
if (ListCount(receivelist) != 0) {
ListFirst(receivelist);
strcpy(msg, ListCurr(receivelist));
ListRemove(receivelist);
printf("%s\n", msg);
}
pthread_mutex_unlock(&receivemut);
}
return 0;
}
*/
void *sender(void *arg) { void *sender(void *arg) {
char *msg; char msg[128];
while (1) { if (client) {
msg = malloc(30); while (1) {
pthread_mutex_lock(&sendmut); pthread_mutex_lock(&sendmut);
if (ListCount(sendlist) != 0) { memset(msg, 0, 128);
ListFirst(sendlist); printf("Type a message: ");
strcpy(msg, ListCurr(sendlist)); fgets(msg, sizeof(msg), stdin);
send(confd, &msg, 30, 0); send(newfd, msg, sizeof(msg), 0);
ListRemove(sendlist); pthread_mutex_unlock(&sendmut);
} }
else { }
pthread_mutex_unlock(&sendmut); else {
continue; while (1) {
} pthread_mutex_lock(&sendmut);
pthread_mutex_unlock(&sendmut); memset(msg, 0, 128);
printf("Type a message: ");
fgets(msg, sizeof(msg), stdin);
send(confd, msg, sizeof(msg), 0);
pthread_mutex_unlock(&sendmut);
}
} }
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) { if (client) {
recv(sockfd, msg, 30, 0); while (1) {
if (msg[0] != '\n' && msg[0] != '\0') {
pthread_mutex_lock(&receivemut); pthread_mutex_lock(&receivemut);
ListAppend(receivelist, msg); memset(buf, 0, 128);
bytes = recv(newfd, buf, 128, 0);
pthread_mutex_unlock(&receivemut); pthread_mutex_unlock(&receivemut);
if (bytes != -1) {
printf("Received: %s\n", buf);
}
else {
perror("recv");
}
} }
} }
return 0; else {
} while (1) {
pthread_mutex_lock(&receivemut);
void *output(void *arg) { memset(buf, 0, 128);
char *msg; bytes = recv(sockfd, buf, 128, 0);
while (1) { pthread_mutex_unlock(&receivemut);
pthread_mutex_lock(&receivemut);
if (ListCount(receivelist) != 0) { if (bytes != -1) {
ListFirst(receivelist); printf("Received: %s\n", buf);
msg = ListCurr(receivelist); }
ListRemove(receivelist); else {
printf("%s\n", (char*)msg); perror("recv");
}
} }
pthread_mutex_unlock(&receivemut);
} }
return 0; return 0;
} }
@@ -107,10 +135,8 @@ int main(int argc, char* argv[]) {
struct addrinfo *servinfo; struct addrinfo *servinfo;
struct sockaddr_storage their_addr; struct sockaddr_storage their_addr;
char s[INET6_ADDRSTRLEN]; char s[INET6_ADDRSTRLEN];
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> \
@@ -135,6 +161,7 @@ int main(int argc, char* argv[]) {
sendlist = ListCreate(); sendlist = ListCreate();
if (atoi(local_port) < atoi(remote_port)) { if (atoi(local_port) < atoi(remote_port)) {
client = 0;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
@@ -190,9 +217,9 @@ 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 {
client = 1;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
@@ -203,7 +230,7 @@ int main(int argc, char* argv[]) {
exit(1); exit(1);
} }
for (q = servinfo; q != NULL; q = q->ai_next) { for (q = servinfo; q != NULL; q = q->ai_next) {
if ((sockfd = socket(q->ai_family, if ((newfd = socket(q->ai_family,
q->ai_socktype, q->ai_socktype,
q->ai_protocol)) == -1) { q->ai_protocol)) == -1) {
perror("Client: socket"); perror("Client: socket");
@@ -211,9 +238,9 @@ 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(newfd, q->ai_addr, q->ai_addrlen) == -1) {
perror("Client: connect"); perror("Client: connect");
close(sockfd); close(sockfd);
continue; continue;
@@ -228,21 +255,19 @@ int main(int argc, char* argv[]) {
else { else {
servinfo = q; servinfo = q;
} }
recv(sockfd, buf, 20, 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(&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(input_thread, NULL);
pthread_join(send_thread, NULL);
pthread_join(receive_thread, NULL);
pthread_join(output_thread, NULL); pthread_join(output_thread, NULL);
*/
*/
pthread_join(send_thread, NULL);
pthread_join(receive_thread, NULL);
close(sockfd); close(sockfd);
close(confd); close(confd);
return 0; return 0;