Watching for a control chracter

This is a discussion on Watching for a control chracter within the Linux Programming forums, part of the Platform Specific Boards category; I have writen a program that watches a port on the local machine and prints out the contents of each ...

  1. #1
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385

    Watching for a control chracter

    I have writen a program that watches a port on the local machine and prints out the contents of each packet it receives to that port.
    The main part of the program is in a function monitorLoop, a while loop usesa variable stayAlive to loop as shown below
    Code:
    int monitorLoop(void)
    {
      //socket setup code
      while (stayAlive == 1)
            {
              //watch the port
             //print the data 
            }
    }
    Wha i want to is to watch for an escape character that will shutdown the server insteas of having to use ^z. ( like telnet watches for ctrl ] ) n

    Obviously i can't use a scanf or anything like that becuase the program would puase until it receives input, can anyone give me an idea on how to do this#?

    using gcc on slackware 10

    mtia

    I have written into the server to watch for a special stop packet but would like to stop it locally aswell
    Monday - what a way to spend a seventh of your life

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Well once you've read the data from wherever you're reading it from - then use a for loop to examine each char in the buffer.

    I must be missing something - this seems too obvious

  3. #3
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    but a ctrl X from the keyboard wouldn't go into the same buffer as the data from the port, unless im missing something really obvious
    Monday - what a way to spend a seventh of your life

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Oh, so you want to read from the network socket as well as from stdin, without blocking on either of them.

    And if there is a ctrl-x on stdin, you want to go away and do some stuff.

    Is that it?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    curses can catch ctrl-x and ctrl-c etc. That's how VI does it.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You could also use signals and catch the ones masked with control c, but the ones that do not have signals associated with them, you could run select on stdin.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    >>Is that it?
    Yes - i want to watch for a ctrl X from the keyboard while constantly watching the port my server runs on. From the information above would curses or signals be more effective?
    Last edited by iain; 11-27-2004 at 05:35 AM.
    Monday - what a way to spend a seventh of your life

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Well it goes something like this
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/select.h>
    #include <termios.h>
    
    void set_nonblock ( ) {
      struct termios newt;
      tcgetattr( 0, &newt );
      newt.c_lflag &= ~( ICANON | ECHO );
      tcsetattr( 0, TCSANOW, &newt );
    }
    
    int main ( ) {
        int status;
    
        set_nonblock();
    
        while ( 1 ) {
            int             max_fd = 0;     /* max fd to be watched - currently only stdin */
            fd_set          read_set;       /* file descriptors to watch */
            struct timeval  tmo;            /* timeout */
    
            FD_ZERO(&read_set);
            FD_SET(0,&read_set);            /* watch for changes on stdin */
            tmo.tv_sec = 1;
            tmo.tv_usec= 0;                 /* wait for up to 1 second */
    
            status = select( max_fd+1, &read_set, 0, 0, &tmo );
            switch ( status ) {
                case -1:
                    /* an error */
                    perror( "select" );
                    break;
                case 0:
                    /* timeout */
                    printf( "select timed out\n" );
                    break;
                default:
                    /* something worth doing - check each fd in each set */
                    if ( FD_ISSET(0,&read_set) ) {
                        char ch;
                        int n = read( 0, &ch, sizeof(ch) );
                        printf( "Read char %c(%d)\n", ch, n );
                    }
                    break;
            }
        }
    
        return 0;
    }
    Add your socket fd's to the read_set and bump max_fd appropriately and add another FD_ISSET() test.

  9. #9
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    thank you salem
    Monday - what a way to spend a seventh of your life

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21