3
Log service : client
- #include <sys/select.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <errno.h>
- #include <string.h>
- #include <signal.h>
- #include <unistd.h>
- #include "debug.h"
- #if defined( DEBUG )
- int debug = 0;
- #endif
- #define HOSTADDR INADDR_LOOPBACK /* 0x7f000001U */
- #define PORTNUM 10123 /* IPPORT_ECHO */
- struct {
- int host_ip;
- int port_num;
- int socket;
- } app;
- void open_conn() {
- int sd = -1;
- struct sockaddr_in sd_address;
- int addrlen = sizeof(struct sockaddr_in);
- if ( (sd = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) == -1 )
- goto error;
- sd_address.sin_family = AF_INET;
- sd_address.sin_addr.s_addr = app.host_ip; /* already in network order */
- sd_address.sin_port = app.port_num;
- if ( connect( sd, (struct sockaddr *) &sd_address, addrlen ) == -1 )
- goto error;
- app.socket = sd;
- return;
- error:
- perror( 0 );
- if ( sd != -1 )
- close( sd );
- exit( 1 );
- }
- int close_conn() {
- if ( app.socket != -1 ) {
- close( app.socket );
- app.socket = -1;
- }
- }
- void quit() {
- close_conn();
- exit( 0 );
- }
- void log_input() {
- char sline[1024];
- if ( fgets( sline, sizeof(sline), stdin ) == 0 )
- quit();
- if ( write( app.socket, sline, strlen( sline ) ) == -1 )
- quit();
- }
- void intr( int sig ) {
- ONDEBUG8( fprintf( stdout, "<sig=%d>\n", sig); );
- signal( sig, intr ); /* catch again */
- }
- void startup() {
- open_conn();
- signal( SIGHUP, intr );
- #if !defined( DEBUG )
- signal( SIGINT, intr );
- #endif
- signal( SIGTERM, intr );
- }
- void init() {
- app.host_ip = htonl( HOSTADDR );
- app.port_num = htons( PORTNUM );
- app.socket = -1;
- }
- void main_loop() {
- fd_set read_fds;
- int n_fds = FD_SETSIZE;
- for ( ;; ) { /* forever */
- FD_ZERO( &read_fds );
- /* standard input? */
- FD_SET( 0, &read_fds );
- switch ( select( n_fds, &read_fds, 0, 0, 0 ) ) {
- case -1: /* trouble */
- ONDEBUG8( fprintf( stdout, "<errno=%d>\n", errno ) );
- if ( errno != EINTR ) {
- perror( 0 );
- quit( 1 );
- }
- break;
- case 0: /* time out */
- break;
- default: /* event */
- if ( FD_ISSET( 0, &read_fds ) )
- log_input();
- break;
- }
- }
- }
- int main( int argc, char **argv ) {
- struct hostent *host;
- int port_num;
- extern int opterr;
- int c;
- init();
- opterr = 0;
- #if defined( DEBUG )
- while ( (c = getopt( argc, argv, "D:h:p:" )) != -1 )
- #else
- while ( (c = getopt( argc, argv, "h:p:" )) != -1 )
- #endif
- switch ( c ) {
- #if defined( DEBUG )
- case 'D':
- debug = atoi( optarg );
- break;
- #endif
- case 'p':
- if ( (port_num = atoi( optarg )) == 0 ) {
- fputs( "portnum?\n", stderr );
- exit( 1 );
- }
- app.port_num = htons( port_num );
- break;
- case 'h':
- if ( (host = gethostbyname( optarg )) == 0 ) {
- fputs( "hostname?\n", stderr );
- exit( 1 );
- }
- app.host_ip = *((unsigned *) host->h_addr);
- endhostent();
- break;
- default:
- #if defined( DEBUG )
- fprintf( stderr, "%s [-D level] [-h host_name] [-p port_num]\n", argv[ 0 ] );
- #else
- fprintf( stderr, "%s [-h host_name] [-p port_num]\n", argv[0] );
- #endif
- exit( 1 );
- }
- setbuf( stdout, 0 );
- startup();
- main_loop();
- }
Comments