Setting socket option so that TCP Modbus slave can re-use same port immediately after freeing it
--- 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;}