You can do better in the server. Let $loop->listen() set up the socket rather than doing it yourself. Also, don't use $newclient->send() as that may block; $self->write() will put data into the Stream's outbound queue to be delivered as normal.
#####################3 server.pl
use strict;
use warnings;
use diagnostics;
use Time::HiRes qw(time);
use IO::Async::Stream;
use IO::Async::Loop;
use Socket qw( SOCK_STREAM );
my $loop = IO::Async::Loop->new();
$loop->listen(
service => 8888,
socktype => SOCK_STREAM,
queuesize => 5,
reuseaddr => 1,
on_accept => sub {
my ( $newclient ) = @_;
$loop->add(
IO::Async::Stream->new(
handle => $newclient,
on_read => sub {
my ( $self, $buffref, $closed ) = @_;
warn 'READ: ', $$buffref;
my $send_start = time;
$self->write( 'ECHO: ' . $$buffref );
warn 'TIME TO SEND TO CLIENT: ', time - $send_start;
$$buffref = '';
return 0;
},
),
);
},
on_resolve_error => sub { print STDERR "Cannot resolve - $_[0]\n"; },
on_listen_error => sub { print STDERR "Cannot listen\n"; },
);
$loop->loop_forever();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|