Initialize socket before main loop
This commit is contained in:
parent
fffd1c3fff
commit
b39e263499
24
main.c
24
main.c
@ -16,9 +16,11 @@
|
||||
* <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static bool exit_requested = false;
|
||||
@ -43,11 +45,31 @@ int main(int argc, char *argv[])
|
||||
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) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
printf("After loop\n");
|
||||
close(sfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user