COMPGROUPS.NET   Search  Post Question   Groups  About  Contact  Register | Login

reading stdin with SELECT? Subscribe

Hi!, i have a "server-like" application:
i use select to accept and recieve data from Sockets.
I want to Add a menu to this app something like "pick an option" thing,
but it has to show data according to data recieved from Sockets..

I found a way to do it with Fork, i mean, First i fork;in the Child i
do all the menu stuff and in the "else" part (the parent part) i have
the rest (the while(1) with a select inside). But i dont like this
because, i don't like forking just for a menu! and i have to create a
socket to communicate with the parent.

i was thinking.. isn't it possible to tell the select to read and show
the stdin, after all, it's a FD.
I tried this but i couldn't get it to work correctly, i ran into
trouble like "recv cant read from a Non-socket fd" "i read the stdin
with "read" but i reads char by char -i guess- and it remains "ready"
so select, keeps selecting stdin forever.

so, i don't know which is THE way to do it correcly if there is such a
thing.

Thanks.

0
Reply ifmusic 5/16/2006 5:47:12 PM Header Report as Spam




ifmusic@gmail.com wrote:
> Hi!, i have a "server-like" application:
> i use select to accept and recieve data from Sockets.
> I want to Add a menu to this app something like "pick an option" thing,
> but it has to show data according to data recieved from Sockets..
>
> I found a way to do it with Fork, i mean, First i fork;in the Child i
> do all the menu stuff and in the "else" part (the parent part) i have
> the rest (the while(1) with a select inside). But i dont like this
> because, i don't like forking just for a menu! and i have to create a
> socket to communicate with the parent.
>
> i was thinking.. isn't it possible to tell the select to read and show
> the stdin, after all, it's a FD.
> I tried this but i couldn't get it to work correctly, i ran into
> trouble like "recv cant read from a Non-socket fd" "i read the stdin
> with "read" but i reads char by char -i guess- and it remains "ready"
> so select, keeps selecting stdin forever.
>
> so, i don't know which is THE way to do it correcly if there is such a
> thing.
>
> Thanks.

This shouldn't be too much of a problem.
Heres a bit of code from something I wrote which could handle that if
fdin was set to stdin.

struct timeval tv = {0,0};
FD_ZERO(&fdset);
FD_SET(fdin,&fdset);
if(select(fdin+1,&fdset,0,0,&tv)>0) {
if(FD_ISSET(fdin,&fdset)) {
i = read(fdin,stdinbuf,STDINBUFSIZE);
if(i>0)
//successful read
len += i;
else
//end of stream or error
}
}
}

If you can't get it working post a bit of code.

0
Reply noogie 5/16/2006 10:47:39 PM Header Report as Spam

jajajajajj!

yes! it works, i had done that befere But when i asked if the FD which
is ready was Stdin i wrote: "if (i=0) .....read......" INSTEAD of "if
(i == 0 ) ...." Simple STUPIDITY .

Thanks !

0
Reply ifmusic 5/17/2006 1:11:14 AM Header Report as Spam

2 Replies
470 Views




Similiar Articles:
















5/18/2012 10:01:57 AM


Reply:
Alert me when someone responds to this posting.