Set SO_REUSEADDR when listening so that we can immediately reuse ports
that are no longer being listened upon, instead of having to wait 60
seconds for the socket to be shutdown after being closed (even on
localhost!).
From 05be3873b985d8d6f401c6e96f2b99927c41e126 Mon Sep 17 00:00:00 2001
From: Josh Gao <[email protected]>
Date: Thu, 24 Jan 2019 17:32:35 -0800
Subject: [PATCH] nc: allow immediate reuse of ports.

Set SO_REUSEADDR when listening so that we can immediately reuse ports
that are no longer being listened upon, instead of having to wait 60
seconds for the socket to be shutdown after being closed (even on
localhost!).
---
 toys/net/netcat.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/toys/net/netcat.c b/toys/net/netcat.c
index 57304f44..fbf3dca8 100644
--- a/toys/net/netcat.c
+++ b/toys/net/netcat.c
@@ -144,6 +144,12 @@ void netcat_main(void)
         }

         sockfd = xsocket(family, type, 0);
+
+        {
+          int val = 1;
+          xsetsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
+        }
+
         if (bind(sockfd, address, bind_addrlen))
           perror_exit("bind");
       }
--
2.20.1.495.gaa96b0ce6b-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to