Determining if stdin stream holds data

This is a discussion on Determining if stdin stream holds data within the C Programming forums, part of the General Programming Boards category; Hello, My question is simple. Since I am working on a small mutl-client server project, I want to be able ...

  1. #1
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Determining if stdin stream holds data

    Hello,

    My question is simple. Since I am working on a small mutl-client server project, I want to be able to type information while the server is running and process what was sent locally. Like if I type in "connections" it will call on a function I already made that will display all clients connected.

    Though, a slight problem is that I have that in my infinite server while loop. This is how my code works:
    Code:
    while (1) {
        /* read local input stream */
        if ((n = read(0, svrMsg, 63)) > 0)
            if (processMessage(svrMsg) == -1)
                break;
    
        /* look for incoming connections */
    
        /* read data accordingly */
    }
    Let me expound. My function processMessage() returns 1 if something generic was sent, and returns -1 if I request "disconnect" which will shut the server down. My read() command is supposed to make sure if something was read, then to process. But my other functions below that are supposed to look for incoming connections and read the data never get executed. It's almost like my read function waits for input, and doesn't move on.

    I have even attempted to flush the input stream:
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    Though no luck. Is there another approach I should take?


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you can use select() to see if stdin has any data for reading. Note this can get platform specific as you'll need to know the file number for stdin.

    Here is an example for the man page for select. Note it can be done outside of unix/linux
    Code:
           #include <stdio.h>
           #include <sys/time.h>
           #include <sys/types.h>
           #include <unistd.h>
    
           int main(void)
           {
               fd_set rfds;
               struct timeval tv;
               int retval;
    
               /* Watch stdin (fd 0) to see when it has input. */
               FD_ZERO(&rfds);
               FD_SET(0, &rfds);
               /* Wait up to five seconds. */
               tv.tv_sec = 5;
               tv.tv_usec = 0;
    
               retval = select(1, &rfds, NULL, NULL, &tv);
               /* Don't rely on the value of tv now! */
    
               if (retval)
                   printf("Data is available now.\n");
                   /* FD_ISSET(0, &rfds) will be true. */
               else
                   printf("No data within five seconds.\n");
    
               return 0;
           }

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Nice,

    Exactly what I was looking for Thantos. Thanks again!


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    You can use fileno(stdin); to get stdin's file descriptor.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  2. Sockets: send/receive continuous stream of data?
    By diddy02 in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-09-2008, 12:52 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM

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