changed socket options and removed infinite loop

This commit is contained in:
2025-06-22 13:45:34 -06:00
parent f6520fa7c0
commit aba9dcdd93
+8 -7
View File
@@ -85,7 +85,12 @@ int main(int argc, char* argv[]) {
perror("Server: socket"); perror("Server: socket");
continue; continue;
} }
/* Allow reuse of socket despite binding */
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
/* Bind */ /* 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);
@@ -109,13 +114,11 @@ int main(int argc, char* argv[]) {
exit(1); exit(1);
} }
while (1) {
sin_size = sizeof their_addr; sin_size = sizeof their_addr;
printf("Waiting for connection...\n"); printf("Waiting for connection...\n");
confd = accept(socketfd, (struct sockaddr *)&their_addr, &sin_size); confd = accept(socketfd, (struct sockaddr *)&their_addr, &sin_size);
if (confd == -1) { if (confd == -1) {
perror("accept"); perror("accept");
continue;
} }
inet_ntop(their_addr.ss_family, inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr), get_in_addr((struct sockaddr *)&their_addr),
@@ -124,8 +127,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 */ /* You're the client */
/* Get remote info */ /* Get remote info */
@@ -168,10 +169,10 @@ 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);
*/
return 0; return 0;
} }