![]() |
| | #1 |
| C Programmer Join Date: Apr 2004
Posts: 477
| Determining if stdin stream holds data 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 */
}
I have even attempted to flush the input stream: Code: while ((ch = getchar()) != '\n' && ch != EOF); - 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 | |
| | #2 |
| & the hat of GPL slaying 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 | |
| | #3 |
| C Programmer 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |