3

Log service : client

  1. #include <sys/select.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <strings.h>
  9. #include <errno.h>
  10. #include <string.h>
  11.  
  12. #include <signal.h>
  13.  
  14. #include <unistd.h>
  15.  
  16. #include "debug.h"
  17.  
  18. #if defined( DEBUG )
  19. int debug = 0;
  20. #endif
  21.  
  22. #define HOSTADDR    INADDR_LOOPBACK     /* 0x7f000001U */
  23. #define PORTNUM     10123               /* IPPORT_ECHO */
  24.  
  25. struct {
  26.     int host_ip;
  27.     int port_num;
  28.     int socket;
  29. } app;
  30.  
  31. void open_conn() {
  32.     int sd = -1;
  33.     struct sockaddr_in sd_address;
  34.     int addrlen = sizeof(struct sockaddr_in);
  35.  
  36.     if ( (sd = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) == -1 )
  37.         goto error;
  38.  
  39.     sd_address.sin_family = AF_INET;
  40.     sd_address.sin_addr.s_addr = app.host_ip; /* already in network order */
  41.     sd_address.sin_port = app.port_num;
  42.  
  43.     if ( connect( sd, (struct sockaddr *) &sd_address, addrlen ) == -1 )
  44.         goto error;
  45.  
  46.     app.socket = sd;
  47.  
  48.     return;
  49.  
  50. error:
  51.  
  52.     perror( 0 );
  53.     if ( sd != -1 )
  54.         close( sd );
  55.     exit( 1 );
  56. }
  57.  
  58. int close_conn() {
  59.     if ( app.socket != -1 ) {
  60.         close( app.socket );
  61.         app.socket = -1;
  62.     }
  63. }
  64.  
  65. void quit() {
  66.     close_conn();
  67.     exit( 0 );
  68. }
  69.  
  70. void log_input() {
  71.     char sline[1024];
  72.  
  73.     if ( fgets( sline, sizeof(sline), stdin ) == 0 )
  74.         quit();
  75.  
  76.     if ( write( app.socket, sline, strlen( sline ) ) == -1 )
  77.         quit();
  78. }
  79.  
  80. void intr( int sig ) {
  81.     ONDEBUG8( fprintf( stdout, "<sig=%d>\n", sig); );
  82.     signal( sig, intr ); /* catch again */
  83. }
  84.  
  85. void startup() {
  86.     open_conn();
  87.  
  88.     signal( SIGHUP, intr );
  89. #if !defined( DEBUG )
  90.     signal( SIGINT, intr );
  91. #endif
  92.     signal( SIGTERM, intr );
  93. }
  94.  
  95. void init() {
  96.     app.host_ip = htonl( HOSTADDR );
  97.     app.port_num = htons( PORTNUM );
  98.     app.socket = -1;
  99. }
  100.  
  101. void main_loop() {
  102.     fd_set read_fds;
  103.     int n_fds = FD_SETSIZE;
  104.  
  105.     for ( ;; ) { /* forever */
  106.         FD_ZERO( &read_fds );
  107.  
  108.         /* standard input? */
  109.         FD_SET( 0, &read_fds );
  110.  
  111.         switch ( select( n_fds, &read_fds, 0, 0, 0 ) ) {
  112.         case -1: /* trouble */
  113.             ONDEBUG8( fprintf( stdout, "<errno=%d>\n", errno ) );
  114.             if ( errno != EINTR ) {
  115.                 perror( 0 );
  116.                 quit( 1 );
  117.             }
  118.             break;
  119.         case 0: /* time out */
  120.             break;
  121.         default: /* event */
  122.             if ( FD_ISSET( 0, &read_fds ) )
  123.                 log_input();
  124.             break;
  125.         }
  126.     }
  127. }
  128.  
  129. int main( int argc, char **argv ) {
  130.     struct hostent *host;
  131.     int port_num;
  132.  
  133.     extern int opterr;
  134.     int c;
  135.  
  136.     init();
  137.  
  138.     opterr = 0;
  139.  
  140. #if defined( DEBUG )
  141.     while ( (c = getopt( argc, argv, "D:h:p:" )) != -1 )
  142. #else
  143.     while ( (c = getopt( argc, argv, "h:p:" )) != -1 )
  144. #endif
  145.         switch ( c ) {
  146. #if defined( DEBUG )
  147.         case 'D':
  148.         debug = atoi( optarg );
  149.         break;
  150. #endif
  151.         case 'p':
  152.             if ( (port_num = atoi( optarg )) == 0 ) {
  153.                 fputs( "portnum?\n", stderr );
  154.                 exit( 1 );
  155.             }
  156.             app.port_num = htons( port_num );
  157.             break;
  158.         case 'h':
  159.             if ( (host = gethostbyname( optarg )) == 0 ) {
  160.                 fputs( "hostname?\n", stderr );
  161.                 exit( 1 );
  162.             }
  163.             app.host_ip = *((unsigned *) host->h_addr);
  164.             endhostent();
  165.             break;
  166.         default:
  167. #if defined( DEBUG )
  168.             fprintf( stderr, "%s [-D level] [-h host_name] [-p port_num]\n", argv[ 0 ] );
  169. #else
  170.             fprintf( stderr, "%s [-h host_name] [-p port_num]\n", argv[0] );
  171. #endif
  172.             exit( 1 );
  173.         }
  174.  
  175.     setbuf( stdout, 0 );
  176.  
  177.     startup();
  178.     main_loop();
  179. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].