Jelajahi Sumber

Test socket against positive value instead of -1

Stéphane Raimbault 2 tahun lalu
induk
melakukan
6914cf96f8
3 mengubah file dengan 7 tambahan dan 7 penghapusan
  1. 2 2
      src/modbus-rtu.c
  2. 4 4
      src/modbus-tcp.c
  3. 1 1
      src/modbus.c

+ 2 - 2
src/modbus-rtu.c

@@ -595,7 +595,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 #endif
 
     ctx->s = open(ctx_rtu->device, flags);
-    if (ctx->s == -1) {
+    if (ctx->s < 0) {
         if (ctx->debug) {
             fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
                     ctx_rtu->device, strerror(errno));
@@ -1135,7 +1135,7 @@ static void _modbus_rtu_close(modbus_t *ctx)
                 (int)GetLastError());
     }
 #else
-    if (ctx->s != -1) {
+    if (ctx->s >= 0) {
         tcsetattr(ctx->s, TCSANOW, &ctx_rtu->old_tios);
         close(ctx->s);
         ctx->s = -1;

+ 4 - 4
src/modbus-tcp.c

@@ -323,7 +323,7 @@ static int _modbus_tcp_connect(modbus_t *ctx)
 #endif
 
     ctx->s = socket(PF_INET, flags, 0);
-    if (ctx->s == -1) {
+    if (ctx->s < 0) {
         return -1;
     }
 
@@ -432,7 +432,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
 /* Closes the network connection and socket in TCP mode */
 static void _modbus_tcp_close(modbus_t *ctx)
 {
-    if (ctx->s != -1) {
+    if (ctx->s >= 0) {
         shutdown(ctx->s, SHUT_RDWR);
         close(ctx->s);
         ctx->s = -1;
@@ -674,7 +674,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
     ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
 #endif
 
-    if (ctx->s == -1) {
+    if (ctx->s < 0) {
         return -1;
     }
 
@@ -704,7 +704,7 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
     ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
 #endif
 
-    if (ctx->s == -1) {
+    if (ctx->s < 0) {
         return -1;
     }
 

+ 1 - 1
src/modbus.c

@@ -371,7 +371,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
         }
     }
 
-    if (ctx->s == -1) {
+    if (ctx->s < 0) {
         if (ctx->debug) {
             fprintf(stderr, "ERROR The connection is not established.\n");
         }