Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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();

In reply to Re^2: Event based handling of multiple connections with AnyEvent by PEVANS
in thread Event based handling of multiple connections with AnyEvent by bennymack

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



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found