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

Pavel Shramov shramov at mexmat.net
Thu Nov 29 11:37:22 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 |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/src/network.c b/src/network.c
index 21b9694..ab04f4c 100644
--- a/src/network.c
+++ b/src/network.c
@@ -683,6 +683,15 @@ static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
 {
 	int loop = 0;
 
+	/* allow multiple sockets to use the same PORT number */
+	if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
+				&loop, sizeof(loop)) == -1) {
+                char errbuf[1024];
+                ERROR ("setsockopt: %s", 
+                                sstrerror (errno, errbuf, sizeof (errbuf)));
+		return (-1);
+	}
+
 	DEBUG ("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