Win32::Socketpair will only work in windows. i need platform independent one. Thread is ok. I tried below code i faced pblm in windows. The Thread is not detaching for context switch. it rotate in while loop. Correct the below code if i am wrong. Server ======
use strict; use IO::Socket; use IO::Select; use Sys::Hostname; use constant BUFSIZE => 1024; my $host = hostname; my $port = shift || '10280'; my $socket = new IO::Socket( Domain => PF_INET, Proto => getprotobyname('tcp'), LocalAddr => $host, LocalPort => $port, Listen => 1,#SOMAXCONN, #ReuseAddr => SO_REUSEADDR, ) or die $@; my $buffer; print "Waiting to do service...\n"; while (my $client =$socket->accept) { print "Client: ",$client->peerhost," Connected..\n"; syswrite($client,"Reached Server\n",BUFSIZE); if(sysread($client,$buffer,BUFSIZE)>0) { print "$buffer\n"; } else { print "Client Disconnected..\n"; print "Waiting to do service...\n"; } }
Client ======
use strict; use IO::Socket; use IO::Select; use Thread::Queue; use constant BUFSIZE => 1024; my $host = shift or die "Usage: client.pl host [port]\n"; my $port = shift || '10280'; my $socket = new IO::Socket(Domain => PF_INET, PeerAddr => $host, PeerPort => $port, Proto => getprotobyname('tcp'), Timeout => 60,) or die $@; my $readers = IO::Select->new() or die "Can't create IO::Select read o +bject"; #$readers->add(\*STDIN); $readers->add($socket); my $Qstdin = new Thread::Queue; async { $Qstdin->enqueue( $_ ) while defined( $_ = <STDIN> ); }->detach; my $buffer; while (1) { my @readers = $readers->can_read(0.1); foreach my $readservice (@readers) { print "Inside can_read\n"; if(sysread($readservice,$buffer,BUFSIZE)>0) { print "$buffer"; } else { while( $Qstdin->pending ) { my $kbinput = $Qstdin->dequeue; syswrite($readservice,$kbinput."\n",BUFSIZE); } } } }

In reply to Re^6: How to Multiplex a Client using IO::Select by muthuma
in thread How to Multiplex a Client using IO::Select by muthuma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.