![]() |
| | #1 |
| Registered User Join Date: Jun 2006
Posts: 16
| Does anyone know which function/library I should use to be able to read a line of user input without Curse? And once I find this function, the following code structure should be able to pick up the keyboard I/O stream right? Code: fd_set rset;
int max_sd=highest_sd++;
while(1) {
FD_SET(0, &rset);
if( select(max_sd, &rset, NULL, NULL, NULL) < 0 ) {
//error code
}
if( FD_ISSET( 0, &rset ) {
//a line has been entered, do something useful here
} else if( FD_ISSET( other_sd, &rset) {....
} else {
//error: select() not blocking
}
Thanks |
| pppbigppp is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| Well it seems like a reasonable start, what sort of effect are you seeing compared to what you expect? Also, how are you reading stdin when select says there is data available? Did you use setbuf/setvbuf on stdin to make it non-blocking before doing any of this. I'm assuming that you're on Linux (or other *IX OS with a proper implementation of select). |
| Salem is offline | |
| | #3 | |
| Registered User Join Date: Jun 2006
Posts: 16
| Quote:
I'm basically trying to demux socket IO and keyboard IO such that a user can send and receive message at the same time. new question: Is it possible to block persistent TCP connection until new data arrives? I'm trying to use one connection for all the message exchange between two network sockets, and I want the process to block until new data is available. select() does not seem to be able to block the socket. I dig up the man page and surely enough, select() unblock when a socket is ready for transmission, not when new data is available. Any idea? What I have currently is this Code: sdTCPChild = accept(sdTCP, ...
while(1) {
FD_SET(sdTCPChild, *rset);
if( select() .... ) {
}
if( FD_ISSET( sdTCPChild, &rset) ) {
//received TCP data, read it, do something useful
//#1
}
}
Thanks! Last edited by pppbigppp; 01-16-2007 at 07:04 AM. | |
| pppbigppp is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > select() unblock when a socket is ready for transmission, not when new data is available. Any idea? Yes, use the correct set. select() can operate on input, output and errors. So use the 'write' set to block on a socket until it is ready to send. |
| Salem is offline | |
| | #5 | |
| Registered User Join Date: Jun 2006
Posts: 16
| Quote:
READ->DISPLAY; KEYBOARD->WRITE(send) The problem is that read() blocks the first time, but for some reason it does not block anymore afterward(which is also why select does not block AFTER the first piece of data arrives). So I end up with this Code: bytesRead = read(sdTCPChild, recBuf, sizeof(recBuf)); //properly blocks until data comes, read non-zero bytes from socket bytesRead = read(sdTCPChild, recBuf, sizeof(recBuf)); //does not block, read 0 bytes. ??? Last edited by pppbigppp; 01-16-2007 at 10:16 AM. | |
| pppbigppp is offline | |
| | #6 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| Did you make the sockets themselves non-blocking? |
| Salem is offline | |
| | #7 | |
| Registered User Join Date: Jun 2006
Posts: 16
| Quote:
Here is some of the socket initiation code Code: //omit a bunch of error checking to save space sdTCP = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); bind(sdTCP, (struct sockaddr *)&sad, sizeof(sad)); listen(sdTCP, QLEN); | |
| pppbigppp is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| Doesn't 0 mean the socket in question has been closed? |
| Salem is offline | |
| | #9 | |
| Registered User Join Date: Jun 2006
Posts: 16
| Quote:
Thanks a bunch! | |
| pppbigppp is offline | |
| | #10 |
| Registered User Join Date: Jun 2006
Posts: 16
| Follow up question: What's the most graceful way to detect broken pipe? Currently, if either the client and the server close() its end of the pipe, select() on the other process will unblock indefinitely and so I end up with infinite loop. Is there a function call that checks up on a file descriptor? |
| pppbigppp is offline | |
| | #11 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| Just a guess, but I said select() can operate on input, output and errors. If you put your sockets / pipes / whatever into the error set as well, then you should get information as to when the unexpected happens. |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| "sorting news" assignment | prljavibluzer | C Programming | 7 | 02-06-2008 06:45 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| I am lost on how to read from file and output to file? | vicvic2477 | C++ Programming | 4 | 02-27-2005 11:52 AM |
| Read Array pro!!Plz help!! | Supra | C Programming | 2 | 03-04-2002 03:49 PM |
| Help! Can't read decimal number | Unregistered | C Programming | 2 | 09-07-2001 02:09 AM |