GuoWenxue
2022-04-22 0fd2ff9f9aca1f30d6b4620f90802223cc221fe0
update socket.c for listen socket
3 files modified
48 ■■■■ changed files
apue/project_socket/main/client_main.c 40 ●●●● patch | view | raw | blame | history
apue/project_socket/src/socket.c 6 ●●●●● patch | view | raw | blame | history
apue/project_socket/src/util_proc.c 2 ●●● patch | view | raw | blame | history
apue/project_socket/main/client_main.c
@@ -15,6 +15,7 @@
#include "socket.h"
#include "sqlite_blob.h"
#include "util_proc.h"
#include "util_time.h"
void print_usage(char *progname)
@@ -40,6 +41,7 @@
    int                  interval = 30; /* default report termperature every 30 seconds */
    int                  sn = 1;        /* default serial number for device ID */
    int                  debug = 0;     /* running in debug mode or not */
    int                  loglevel = LOG_LEVEL_INFO;
    char                *logfile="client.log";
    socket_ctx_t         sock;
@@ -75,6 +77,8 @@
            case 'd':
                debug=1;
                logfile="stdout";
                loglevel=LOG_LEVEL_DEBUG;
                break;
            case 'I':
@@ -98,21 +102,15 @@
        return 0;
    }
    if( debug )
    {
        /* set logger to standard output with level debug */
        logger_init(NULL, LOG_LEVEL_DEBUG);
        log_info("set program running on debug now.\n");
    }
    else
    {
        /* set logger to $logfile with level info */
        if( logger_init(logfile, LOG_LEVEL_INFO) < 0 )
    if( logger_init(logfile, loglevel) < 0 )
        {
            fprintf(stderr, "Initial logger file '%s' failure: %s\n", logfile, strerror(errno));
            return 1;
        }
    if( !debug )
    {
        log_info("set program running on background now.\n");
        daemon(1, 1); /* don't change work path, don't close opened file descriptor */
    }
@@ -175,19 +173,24 @@
            }
        }
        /* socket disconnected */
        /* +-------------------------------+
         * |      socket disconnect        |
         * +-------------------------------+*/
        if( sock.fd < 0 )
        {
            if( sample_flag )
            {
                blobdb_push_packet(pack_buf, pack_bytes);
            }
            continue;
        }
        else /* socket connected */
        {
            /* +---------------------------------+
             * |   socket send sample packet     |
             * +---------------------------------+*/
        /* +-------------------------------+
         * |      socket connected         |
         * +-------------------------------+*/
        /*  socket send sample packet */
            if( sample_flag )
            {
                log_debug("socket send sample packet bytes[%d]: %s\n", pack_bytes, pack_buf);
@@ -199,9 +202,7 @@
                }
            }
            /* +---------------------------------+
             * | socket send packet in database  |
             * +---------------------------------+*/
        /*  socket send packet in database  */
            if( !blobdb_pop_packet(pack_buf, sizeof(pack_buf), &pack_bytes) )
            {
                log_debug("socket send database packet bytes[%d]: %s\n", pack_bytes, pack_buf);
@@ -216,9 +217,8 @@
                    blobdb_del_packet();
                }
            }
        }
        sleep(1);
        msleep(100);
    }
    socket_term(&sock);
apue/project_socket/src/socket.c
@@ -41,13 +41,14 @@
 */
int socket_init(socket_ctx_t *sock, char *host, int port)
{
    if( !sock || !host || port<=0 )
    if( !sock || port<=0 )
        return -1;
    memset( sock, 0, sizeof(*sock) );
    sock->fd = -1;
    strncpy(sock->host, host, HOSTNAME_LEN);
    sock->port = port;
    if( host ) /* server no need it */
        strncpy(sock->host, host, HOSTNAME_LEN);
}
/*  description: close socket
@@ -83,6 +84,7 @@
    if( !sock )
        return -1;
    set_socket_rlimit(); /* set max open socket count */
}
/*  description: socket connect to server in block mode
apue/project_socket/src/util_proc.c
@@ -356,7 +356,7 @@
 * *****************************************************************************/
int set_daemon_running(const char *pid_file)
{
    daemonize(0, 1);
    daemon(1, 1);
    log_info("Program running as daemon [PID:%d].\n", getpid());
    if (record_daemon_pid(pid_file) < 0)