Thread: Timing programs

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    Timing programs

    Ok. I read the FAQ, but it was fairly vague. I'm trying to create a program that will run until the user inputs 'x'. So this is how I tried to implement it. I also used Salems suggestions on how to pass info from the file into an array to establish my socket. Mind checking over my code and telling me where I should improve on it? I'm used to MUD code, and a lot of this stuff is already in there.

    Code:
    #include <errno.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    extern int errno;
    extern int h_errno;
    typedef struct  config_data	CONFIG_DATA;
    
    struct config_data
    {
      	char *host;
      	int port;
      	char *path;
    };
    
    void load_config( void )
    {
    	FILE_DATA * fp;
    	CONFIG_DATA *cData;
    	int cnt;
    	char buffer[5000];
    
    	if (( fp = fopen("config.txt", "rb" )) == NULL )
    	{
    		perror("no config found.");
    	    return 0;
      	}
    
      	for ( cnt = 0; cnt < 3; cnt++ )
      	{
    		while ( buffer = fgetc( fp ) ) != EOF )
    		{
    			cData->host = str_dup( buffer );
    			cData->port = str_dup( buffer );
    			cData->path = str_dup( buffer );
    		}
    	}
    }
    
    void check_server( void )
    {
    	char                 buffer[4096];
        struct hostent      *hostaddr;
        int                  port;
        struct protoent     *protocol;
        int                  rval;
        int                  sd;
        struct sockaddr_in   socketaddr;
        CONFIG_DATA * cData;
    
        if ( argc[0] != '\0' )
        {
          printf( "no arguments needed.\n" );
          return (EINVAL); /* Invalid argument */
        }
    
        /* quick sanity check */
    
        port = atoi( cData->port )
    
        if ( port < 1 || port > 65535 )
        {
          printf( "client: invalid port number\n" );
          return (EINVAL);
        }
    
        /*
         * Build our socket
         */
    
        protocol = getprotobyname( "tcp" );
        if ( !protocol )
        {
          perror( "getprotobyname()" );
          return (errno);
        }
    
        sd = socket( PF_INET, SOCK_STREAM, 0 );
        if ( sd == -1 )
        {
          perror( "socket()" );
          return (errno);
        }
    
        /*
         * Setup info about the remote host
         */
    
        memset( &socketaddr, 0, sizeof(socketaddr) );
    
        socketaddr.sin_family = AF_INET;
        socketaddr.sin_port = htons( port );
    
        hostaddr = gethostbyname( cData->host );
    
        if ( !hostaddr )
        {
          fprintf( stderr, "gethostbyname(): %s\n", hstrerror(h_errno) );
          return (h_errno);
        }
    
        /* copy address from hostaddr to socketaddr */
    
        memcpy( &socketaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length );
    
        /*
         * Connect to the host
         */
    
        rval = connect( sd, (struct sockaddr *) &socketaddr, sizeof(socketaddr) );
    
    	if ( rval == -1 ) // Servers down, boot it
        {
    
    		spawnv( P_WAIT, cData->path, argv[0] );
    		close( sd );
        	return (0);
      	}else{
    
    	close( sd );
    	return (0);
    }
    
    int main ( int argc, char * argv[])
    {
    	clock_t timer;
    
    
    	load_config( void );
    
    	timer = time( );
    
    	while ( argv[0] != 'x' )
    	{
    		if ( timer == 300 )
    		{
    			puts("Checking server.");
    			check_server( void );
    			timer = 0;
    			timer = time( );
    		}
    	}
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Event driven programming can be done quite easily without tracking the time. You may want to revise your input/output design though. You can still use the command-line arguments, but once main exits, the user will need to run the program again.

    So you need to ask the user explicitly if he wants to quit, and then return from main. Depending on your design it can be as simple as this while loop:
    Code:
    int mode;
    while( (mode = getchar()) != 'x') {
        puts("Checking server.");
        checkserver();
        fputs("Quit? ", stdout);
    }
    Last edited by whiteflags; 07-08-2006 at 04:01 PM.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Well the time is there so every 5 minutes it checks the server status, and if its down, boots it. So thats why i've got the time there. Unless there is a way you can continueosly run the check so as soon as it goes down, it'll now and boot the program.

    I just basically want the thing to keep running until the user tells it to stop running, or he exits the program.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you only want to check every few minutes...
    Code:
    while ( 1 ) {
      checkServer();
      sleep( 5*60 );  /* 5 mins */
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timing the C
    By stabu in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 06:29 AM
  2. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  3. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  4. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 12:43 AM