Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You can work around it with a select loop. The trick is that although the socket is still blocking it only *actually blocks* if you *actually try to read from it when there is no data*. By using vec and the 4 arg select as shown you can check for data before you go for a blocking read. If there is actual data there it won't actually block, so it is *effectively non blocking* rather than actually non blocking.

Here is a sample server. Note that because there is vitually no delay between the socket being readable and actually reading it you will only get 1 byte of data as sysread is not buffering. You can do this with the IO::Select can_read() method if you want the OO interface.

use Socket; $host = pack('C4', 127,0,0,1); $port = 8888; $proto = 6; # TCP $queueSize = 5; # Queue up to 5 connections $pollTime = 0.5; # polling time $delay = 0.5; # slow the select loop down for example socket( SOCK, AF_INET, SOCK_STREAM, $proto ); $address = pack('S n a4 x8', AF_INET, $port, $host); bind(SOCK, $address); listen(SOCK, $queueSize); print STDOUT "Server host: ",join('.',unpack('C4', $host)),"\n"; print STDOUT "Server port: $port\n"; $cAddress = accept(NEWSOCK,SOCK); ($cDomain, $cPort, $cHost) = unpack('S n a4 x8', $cAddress); print STDOUT "Client host: ",join('.',unpack('C4', $cHost)),"\n"; print STDOUT "Client port: $cPort\n"; select(NEWSOCK); $| = 1; select(STDOUT); print NEWSOCK "Welcome to Reverse Echo Server.\r\n"; vec($bits1,fileno(NEWSOCK),1)=1; while(1) { $rc=select($rout1=$bits1,$wout1=$bits1,$eout1=$bits1,$pollTime); +# poll print "$rc=select($rout1,$wout1,$eout1)\n"; if ( vec($rout1,fileno(NEWSOCK),1) ) { sysread( NEWSOCK, $buf, 1 ); print "Got $buf\n"; } select(undef,undef,undef,$delay); # this is not a select, it is a + sleep! } close(NEWSOCK); close(SOCK); exit; __DATA__ # telnet localhost 8888 Welcome to Reverse Echo Server. Hello # server C:\> Server host: Server port: 8888 Client host: Client port: 3432 1=select( ,?, ) 1=select( ,?, ) 1=select( ,?, ) Got H 2=select(?,?, ) Got e 2=select(?,?, ) Got l 2=select(?,?, ) Got l 2=select(?,?, ) Got o 1=select( ,?, ) 1=select( ,?, ) 1=select( ,?, )



In reply to Re: IO::Socket::INET -- Jettero verses non-blocking in windows by tachyon
in thread IO::Socket::INET -- jettero verses non-blocking in windows by jettero

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (2)
    As of 2018-10-20 17:08 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (119 votes). Check out past polls.