From 5ba3cb56c2832f5885bd05be9ed88a9434451c21 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 8 May 2020 17:02:31 +0200 Subject: Server socket should be now reusable --- plop.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'plop.c') diff --git a/plop.c b/plop.c index d676c4f..e90c3b0 100644 --- a/plop.c +++ b/plop.c @@ -36,6 +36,7 @@ int make_server(const char * node, const char * service) } int server; + int optval = 1; for (it = result; it != NULL; it = it->ai_next) { @@ -44,6 +45,12 @@ int make_server(const char * node, const char * service) if (-1 == server) continue; + if (-1 == setsockopt(server, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof(optval))) + { + close(server); + continue; + } + if (-1 == fcntl(server, F_SETFL, (fcntl(server, F_GETFL, 0) | O_NONBLOCK))) { close(server); @@ -177,6 +184,11 @@ int main(int argc, char ** argv) fdv[0].events = POLLIN; fdc++; + if (-1 == fdv[0].fd) + { + return 1; // TODO: Document error codes/print usage information + } + while (0 < fdc && -1 != poll(fdv, fdc, -1)) { int shift_by = 0; -- cgit v1.1