If the previous invocation of the process happened just a while ago
(eg. the daemon was restarted), the listening socket might still be in
TIME_WAIT state, which prevents registering a new listening socket to
the port. This can be avoided by using the SO_REUSEADDR option which
instructs bind() call to bind to the port anyway.
Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
struct sockaddr_in addr;
struct listening_socket incoming;
int sockfd, ret;
+ int enable = 1;
bzero(&addr, sizeof(addr));
bzero(&incoming, sizeof(incoming));
goto out;
}
+ ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
+ if (ret < 0)
+ printf("Error setting SO_REUSEADDR: %m\n");
+
addr.sin_family = AF_INET;
addr.sin_port = htons(8347);
addr.sin_addr.s_addr = INADDR_ANY;