C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2005, 01:53 PM   #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.
Stack Overflow is offline   Reply With Quote
Old 01-28-2005, 01:57 PM   #2
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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;
       }
Thantos is offline   Reply With Quote
Old 01-28-2005, 02:06 PM   #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.
Stack Overflow is offline   Reply With Quote
Old 01-28-2005, 02:52 PM   #4
Gawking at stupidity
 
Join Date: Jul 2004
Posts: 2,324
You can use fileno(stdin); to get stdin's file descriptor.
__________________
If you understand what you're doing, you're not learning anything.

Ignore any "advice" esbo tries to give you. It's wrong.
itsme86 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:58 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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