commit: 279c8148e8c823d4798c059230fdb96f1f694caf
parent: e60326b350f234ad7dbf93be8b4a943d38935f7c
author: Chris Noxz <chris@noxz.tech>
date: Sun, 4 Aug 2019 20:49:36 +0200
Reuse address and port for socket
1 file changed, 13 insertions(+)
diff --git a/swps.c b/swps.c
@@ -245,6 +245,7 @@ main(int argc, char **argv)
{
static struct sockaddr_in s_addr;
int port, filefd;
+ int reuse = 1;
/* validate arguments */
if (argc != 3 || !strcmp(argv[1], "-h"))
@@ -263,6 +264,18 @@ main(int argc, char **argv)
/* setup the network socket */
if ((serverfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die(1, "Error: system call 'socket'\n");
+
+ /* reuse address and port */
+ if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR,
+ (const char*)&reuse, sizeof(reuse)) < 0)
+ die(1, "Error: system call 'setsockopt(SO_REUSEADDR)'\n");
+ #ifdef SO_REUSEPORT
+ if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEPORT,
+ (const char*)&reuse, sizeof(reuse)) < 0)
+ die(1, "Error: system call 'setsockopt(SO_REUSEPORT)'\n");
+ #endif
+
+ /* bind and listen */
if (bind(serverfd, (struct sockaddr *)&s_addr, sizeof(s_addr)) < 0)
die(1, "Error: system call 'bind'\n");
if (listen(serverfd, 64) < 0)