From b74ad5954607d34e939f9cf32876bd7ba65998f9 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Thu, 11 Apr 2024 13:08:05 +0800
Subject: [PATCH] Update socket example project

---
 project/socketd/sock_client.c |   26 +++++++++-----------------
 1 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/project/socketd/sock_client.c b/project/socketd/sock_client.c
index 20f1898..6a594d9 100644
--- a/project/socketd/sock_client.c
+++ b/project/socketd/sock_client.c
@@ -69,7 +69,9 @@
     char                 pack_buf[1024];
     int                  pack_bytes = 0;
     pack_info_t          pack_info;
-    pack_proc_t          pack_proc = packet_segmented_pack; /* use string packet */
+    pack_proc_t          pack_proc = packet_segmented_pack; /* use segmented string packet */
+  //pack_proc_t          pack_proc = packet_json_pack;      /* use JSON string packet */
+  //pack_proc_t          pack_proc = packet_tlv_pack;       /* use TLV(Tag Length Value) packet */
 
     struct option opts[] = {
         {"ipaddr", required_argument, NULL, 'i'},
@@ -180,25 +182,15 @@
          * +---------------------------------+*/
 
         /* start connect to server if not connected */
-        if( sock.fd < 0 )
+        if( !socket_connected(&sock) )
         {
             socket_connect(&sock);
-        }
-
-        /* check socket connected or not  */
-        if( sock_check_connect(sock.fd) < 0 )
-        {
-            if( sock.fd > 0 )
-            {
-                log_error("socket got disconnected, terminate it and reconnect now.\n");
-                socket_term(&sock); /* close the soket */
-            }
         }
 
         /* +-------------------------------+
          * |      socket disconnect        |
          * +-------------------------------+*/
-        if( sock.fd < 0 )
+        if( !socket_connected(&sock) )
         {
             if( sample_flag )
             {
@@ -220,7 +212,7 @@
             {
                 log_warn("socket send sample packet failure, save it in database now.\n");
                 database_push_packet(pack_buf, pack_bytes);
-                socket_term(&sock); /* close the soket */
+                continue;
             }
         }
 
@@ -231,7 +223,7 @@
             if( socket_send(&sock, pack_buf, pack_bytes) < 0 )
             {
                 log_error("socket send database packet failure");
-                socket_term(&sock); /* close the soket */
+                continue;
             }
             else
             {
@@ -240,7 +232,7 @@
             }
         }
 
-        msleep(50);
+        msleep(5);
     }
 
 cleanup:
@@ -259,7 +251,7 @@
 
     time(&now);
 
-    if( now >= *last_time+interval )
+    if( difftime(now, *last_time)>interval )
     {
         need = 1; /* need sample now  */
         *last_time = now;

--
Gitblit v1.9.1