accept input from user

This commit is contained in:
Dane MacFadden
2025-06-24 16:46:10 -06:00
parent 3a40ecc2aa
commit 52644a10c8
+9 -17
View File
@@ -16,18 +16,20 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <signal.h> #include <signal.h>
#include <list.h> /*#include "list.h"*/
#define MAXBUFSIZE 30 #define MAXBUFSIZE 30
#define BACKLOG 10 #define BACKLOG 10
LIST *receivelist, *sendlist; /*LIST *receivelist, *sendlist;*/
void *input(void *arg) { void *input(void *arg) {
char msg[512];
while (1) { while (1) {
/* printf("Type a message: ");
* Accept input scanf("%s", msg);
*/ printf("%s", msg);
} }
return 0; return 0;
} }
@@ -72,11 +74,10 @@ int main(int argc, char* argv[]) {
remote_machine = argv[2]; remote_machine = argv[2];
remote_port = argv[3]; remote_port = argv[3];
receivelist = ListCreate(); /*receivelist = ListCreate();
sendlist = ListCreate(); sendlist = ListCreate();
*/
if (atoi(local_port) < atoi(remote_port)) { if (atoi(local_port) < atoi(remote_port)) {
/* Congrats, you're the server */
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;
@@ -87,7 +88,6 @@ int main(int argc, char* argv[]) {
exit(1); exit(1);
} }
/* Create socket */
for (p = servinfo; p != NULL; p = p->ai_next) { for (p = servinfo; p != NULL; p = p->ai_next) {
if ((socketfd = socket(p->ai_family, if ((socketfd = socket(p->ai_family,
p->ai_socktype, p->ai_socktype,
@@ -95,13 +95,11 @@ int main(int argc, char* argv[]) {
perror("Server: socket"); perror("Server: socket");
continue; continue;
} }
/* Allow reuse of socket despite binding */
if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) { sizeof(int)) == -1) {
perror("setsockopt"); perror("setsockopt");
exit(1); exit(1);
} }
/* Bind */
if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { if (bind(socketfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
close(socketfd); close(socketfd);
perror("Server: bind"); perror("Server: bind");
@@ -137,8 +135,6 @@ int main(int argc, char* argv[]) {
printf("Getting chatty with %s\n", s); printf("Getting chatty with %s\n", s);
} }
else { else {
/* You're the client */
/* Get remote info */
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;
@@ -149,7 +145,6 @@ 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) {
/* Create socket */
if ((socketfd = socket(q->ai_family, if ((socketfd = socket(q->ai_family,
q->ai_socktype, q->ai_socktype,
q->ai_protocol)) == -1) { q->ai_protocol)) == -1) {
@@ -177,11 +172,8 @@ int main(int argc, char* argv[]) {
} }
printf("Connected!\n"); printf("Connected!\n");
} }
/*
pthread_create(&input_thread, NULL, input, (void*) msg); pthread_create(&input_thread, NULL, input, (void*) msg);
pthread_join(input_thread, NULL); pthread_join(input_thread, NULL);
*/
close(socketfd); close(socketfd);
close(confd); close(confd);