Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: AnyEvent tcp_server not working

by kikuchiyo (Friar)
on Apr 03, 2020 at 22:21 UTC ( #11115017=note: print w/replies, xml ) Need Help??

in reply to AnyEvent tcp_server not working

The problem is that the  my $handle variable is private to the callback sub, and is not referenced anywhere else. So when that sub returns (immediately after push_writing your "Hello" message), it will be destroyed, and with it, your connection.

A solution is including a reference to it in one of AE::Handle's callbacks, e.g.:

on_error => sub { my ($hdl, $fatal, $msg) = @_; AE::log error => $msg; $hdl->destroy(); undef $handle; # <------ },

This way, the callback sub closes over the variable, so its reference count will be nonzero when the sub returns and thus your connection will stay alive.

Replies are listed 'Best First'.
Re^2: AnyEvent tcp_server not working
by navalned (Beadle) on Apr 03, 2020 at 22:32 UTC
    That did it. I need to go back through the manuals. I don't think I read anything that mentioned this. Thanks!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115017]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-08-18 02:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found