[collectd] [PATCH] set SO_REUSEADDR on socket so multiple listeners can share port

Pavel Shramov shramov at mexmat.net
Thu Nov 29 11:34:09 CET 2007


From: Pavel Shramov <sharmov at mexmat.net>

This patch allows other programs to bind same port and process data from clients
not interfering with master collectd server.
---
 src/network.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/src/network.c b/src/network.c
index e9ba84b..ff4ac51 100644
--- a/src/network.c
+++ b/src/network.c
@@ -123,6 +123,13 @@ static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
 {
 	int loop = 1;
 
+	/* allow multiple sockets to use the same PORT number */
+	if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
+				&loop, sizeof(loop)) == -1) {
+		syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
+		return (-1);
+	}
+
 	DBG ("fd = %i; calling `bind'", se->fd);
 
 	if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
-- 
1.5.3.5




More information about the collectd mailing list