Browse Source

New API to close slave TCP socket

Stéphane Raimbault 15 years ago
parent
commit
5f65efd83f
3 changed files with 13 additions and 6 deletions
  1. 8 1
      src/modbus.c
  2. 2 0
      src/modbus.h
  3. 3 5
      tests/bandwidth-slave-many-up.c

+ 8 - 1
src/modbus.c

@@ -1460,7 +1460,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
 
            Timeouts are ignored in canonical input mode or when the
            NDELAY option is set on the file via open or fcntl */
-        mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY);
+        mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
         if (mb_param->fd < 0) {
                 perror("open");
                 printf("ERROR Can't open the device %s (%s)\n",
@@ -1924,6 +1924,13 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket)
         return mb_param->fd;
 }
 
+/* Closes a TCP socket */
+void modbus_slave_close_tcp(int socket)
+{
+        shutdown(socket, SHUT_RDWR);
+        close(socket);
+}
+
 /** Utils **/
 
 /* Sets many input/coil status from a single byte value (all 8 bits of

+ 2 - 0
src/modbus.h

@@ -326,6 +326,8 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
 void modbus_slave_manage(modbus_param_t *mb_param, const uint8_t *query,
                          int query_length, modbus_mapping_t *mb_mapping);
 
+/* Closes a TCP socket */
+void modbus_slave_close_tcp(int socket);
 
 /**
  * UTILS FUNCTIONS

+ 3 - 5
tests/bandwidth-slave-many-up.c

@@ -32,11 +32,10 @@ modbus_mapping_t mb_mapping;
 
 static void close_sigint(int dummy)
 {
-        shutdown(slave_socket, SHUT_RDWR);
-        close(slave_socket);
+        modbus_slave_close_tcp(slave_socket);
         modbus_mapping_free(&mb_mapping);
 
-	exit(dummy);
+        exit(dummy);
 }
 
 int main(void)
@@ -115,8 +114,7 @@ int main(void)
                                         } else {
                                                 /* Connection closed by the client, end of server */
                                                 printf("Connection closed on socket %d\n", master_socket);
-                                                shutdown(master_socket, SHUT_RDWR);
-                                                close(master_socket);
+                                                modbus_slave_close_tcp(master_socket);
 
                                                 /* Remove from reference set */
                                                 FD_CLR(master_socket, &refset);