Initialize socket before main loop

This commit is contained in:
Camden Dixie O'Brien 2022-10-13 12:06:25 +01:00
parent fffd1c3fff
commit b39e263499

24
main.c
View File

@ -16,9 +16,11 @@
* <https://www.gnu.org/licenses/>. * <https://www.gnu.org/licenses/>.
*/ */
#include <netinet/in.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
static bool exit_requested = false; static bool exit_requested = false;
@ -43,11 +45,31 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
int sfd = socket(AF_INET6, SOCK_STREAM, 0);
if (sfd == -1) {
fprintf(stderr, "Failed to open socket\n");
return 1;
}
const struct sockaddr_in6 haddr = {
.sin6_family = AF_INET6,
.sin6_port = htons(7070),
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
};
if (bind(sfd, (const struct sockaddr *)&haddr, sizeof(haddr)) == -1) {
fprintf(stderr, "Error binding socket to address\n");
return 1;
}
if (listen(sfd, 8) == -1) {
fprintf(stderr, "Error attempting to listen on socket\n");
return 1;
}
while (!exit_requested) { while (!exit_requested) {
sleep(1); sleep(1);
} }
printf("After loop\n"); close(sfd);
return 0; return 0;
} }