in reply to Multiplexing problem

You might want to see Re: Please suggest a non-forking way to do this (OS: windows) (solution), if your filehandles are pipes. Also see Non-blocking Reads from Pipe Filehandle. Otherwise show your code, or a chopped down version.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku

Replies are listed 'Best First'.
Re^2: Multiplexing problem
by ikegami (Patriarch) on Feb 08, 2010 at 20:16 UTC
    It would be simpler to sockify STDIN. Create a socket pair, and have a thread dump STDIN into one end of the pair. The other end is used in lieu of STDIN in the main thread.
      how can i do that?
      How can he use pipes to his advance?
        He can't if he wants to use select. The problem is that he is currently using a pipe (or whatever type of handle STDIN is). He needs to use a socket instead of a pipe because they're not selectable in Windows.
Re^2: Multiplexing problem
by grlemn (Initiate) on Feb 08, 2010 at 19:39 UTC
    Here is my code:
    ---server--- use IO::Socket::INET; use IO::Select; $socket=new IO::Socket::INET->new(LocalPort=>2010,Proto=>'udp'); $select=IO::Select->new($socket,\*STDIN); LOOP: while(1) { @readers=$select->can_read(); foreach $ready (@readers) { if($ready eq \*STDIN) { print "\n","Enter message to send to client : "; $msg_out=<STDIN>; $socket->send($msg_out); goto LOOP; } elsif($ready eq $socket) { $socket->recv($msg_in,100); print "\n","Peer>",$msg_in; goto LOOP; } } } exit 1; ---client--- use IO::Socket::INET; use IO::Select; $socket=new IO::Socket::INET->new(PeerPort=>2010,Proto=>'udp',PeerAddr +=>'localhost'); $select=IO::Select->new($socket,\*STDIN); LOOP: while(1) { @readers=$select->can_read(); foreach $ready (@readers) { if($ready eq \*STDIN) { print "Enter message to send to server : "; $msg_out=<STDIN>; $socket->send($msg_out); goto LOOP; } elsif($ready eq $socket) { $socket->recv($msg_in,100); print "\n","Peer>",$msg_in; goto LOOP; } } } exit 1;

      STDIN is not a socket. You can only use select on sockets in Windows, as previously stated.

      Even if you weren't on Windows, it would be defy the purpose of select to read until you got a newline, and it doesn't make sense to use buffered I/O and select on the same handle.

        Another code:
        use IO::Socket::INET; use IO::Select; syswrite(STDOUT,"----- Server Program -----\n"); $socket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp') or die + "socket() error: $!"; $select=IO::Select->new($socket); LOOP: while(1) { if(@readers=$select->can_read(0.1)) { $socket->recv($msg_in,128); syswrite(STDOUT,"\nPeer>$msg_in"); } else { syswrite(STDOUT,"Enter message to send to server : "); sysread(STDIN,$msg_out,100); send($socket,$msg_out,0,'localhost'); goto LOOP; } } exit 1;
        ==============================
        use IO::Socket::INET; use IO::Select; syswrite(STDOUT,"----- Client Program -----\n"); $socket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',PeerAddr +=>'localhost') or die "socket() error: $!"; $select=IO::Select->new($socket); LOOP: while(1) { if(@readers=$select->can_read(0.1)) { $socket->recv($msg_in,128); syswrite(STDOUT,"\nPeer>$msg_in"); } else { syswrite(STDOUT,"Enter message to send to server : "); sysread(STDIN,$msg_out,100); $socket->send($msg_out); goto LOOP; } } exit 1;
        But when i send messages from client the server doesn't print the data on screen but in order to print it i have to change line(newline).It acts like it buffers the data.I would appreciate any ideas.