Setting socket option so that TCP Modbus slave can re-use same port immediately after freeing it
authorEdouard Tisserant
Mon, 11 Feb 2019 11:07:01 +0100
changeset 3 1223f413e054
parent 2 19bf8e678c42
child 4 99009b24d401
child 7 9334c8280602
Setting socket option so that TCP Modbus slave can re-use same port immediately after freeing it
sin_util.c
--- a/sin_util.c	Fri Nov 16 09:28:12 2018 +0100
+++ b/sin_util.c	Mon Feb 11 11:07:01 2019 +0100
@@ -153,6 +153,10 @@
   if ((type = gettypebyname(protocol)) == SOCK_PACKET)       return -1;
   /* create the socket */
   if ((s = socket(PF_INET, type, 0)) < 0)                   {perror("socket()");  return -1;}   
+  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0) {
+      perror("setsockopt(SO_REUSEADDR) failed");
+      return -1;
+  }
   /* bind the socket */
   if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0)   {perror("bind()");    return -1;}