⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11-8.c

📁 《Linux应用开发技术详解》附书光盘中的例程。
💻 C
字号:
#include <stdio.h>
      #include <stdlib.h>
      #include <unistd.h>
      #include <string.h>
      #include <errno.h>
      #include <signal.h>
      #include <sys/wait.h>
      #include "afxdef.h"
      #include "typedef.h"
      #include "bsocket.h"
      #include "utilex.h"
      #include "online.h"
      #include "mon.h"
     
     /* Declare program run flag */
     extern int g_runflag;
     void run_service();
     void request_trans(SOCKET* sock, char* ip);
     int main(void)
      {
              int pid;
     
              /* Set signal transaction */
              signal(SIGTERM, sig_action);
              //signal(SIGINT,  sig_action);
              signal(SIGPIPE, sig_action);
              signal(SIGQUIT, sig_action);
             signal(SIGHUP,  sig_action);
              signal(SIGCHLD, clean_childpid);
              /* Init configuration */
             if ( init_config() != 0 ) {
                      fprintf(stderr, "Fail to init configuration ... \n");
                      return -1;
              }
              if ( (pid = fork()) == 0 ) {
                      signal(SIGTERM, SIG_IGN);
                      update_service();
                      exit(0);
              }
              else {
                      if ( pid == -1 ) {
                              fprintf(stderr, "Fail to fork shared memory process \n");
                              exit(-1);
                      }
     
                      sleep(1);
                      /* Run monitor service */
                      run_service();
              }
     
              return 0;
      }
     
     
83 void run_service()
      {
              SOCKET lis_sock;
              SOCKET cli_sock;
              char fromip[20];
     
              if ( (lis_sock = create_sock(AF_INET, SOCK_STREAM, 0)) == -1 ) {
                      fprintf(stderr, "Fail to create socket \n");
                      return;
              }
              if ( bind_sock(lis_sock, LIS_PORT) != 0 ) {
                      fprintf(stderr, "Fail to bind socket \n");
                      return;
              }
     
              if ( listen_sock(lis_sock, 15) != 0 ) {
                     fprintf(stderr, "Fail to listen port \n");
                     return;
             }
    
             while ( g_runflag ) {
    
                     if ( (cli_sock = accept_sock(lis_sock, fromip)) == -1 ) {
    
                             if ( errno == EINTR )
                                     continue;
                             else {
                                     fprintf(stderr, "Fail to accept socket \n");
                                     break;
                             }
                     }
    
                     if ( fork() == 0 ) {
                             /* Request transaction */
                             signal(SIGTERM, SIG_IGN);
                             request_trans(&cli_sock, fromip);
                             exit(0);
                     }
                     else
                        close_sock(&cli_sock);
    
             } // end while
    
             close_sock(&lis_sock);
             return;
     }
    
    
     void request_trans(SOCKET* sock, char* ip)
     {
             int rs;
    
             rs = mon_login(sock);
             if ( rs == 0 ) { /* pass authentication */
    
                     do {
                             rs = mon_fetch(sock);
                     } while ( !rs );
             }
    
             close_sock(sock);
             return;
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -