Thanks Tye, oiskuu and others, for midwifing my first server code example.

Here is the final, working code. It correctly handles the initial connection and the client closing the connection, or being killed.

# server use Modern::Perl; use EV; use AnyEvent; use IO::Socket::INET; my $is_connected; my $project = my $config = my $text = my $this_engine = {} ; $config->{remote_control_port} = 57000; start_remote(); EV::run(); sub start_remote { my $port = $config->{remote_control_port}; say "Starting remote control listener on port $port"; $project->{remote_control_socket} = IO::Socket::INET->new( LocalAddr => 'localhost', LocalPort => $port, Proto => 'tcp', Type => SOCK_STREAM, Listen => 1, Reuse => 1) || die $!; start_watcher(); } sub start_watcher { $this_engine->{events}->{remote_control} = AE::io( $project->{remote_control_socket}, 0, \&process_remote_command + ) } sub remove_watcher { undef $this_engine->{events}->{remote_control}; } sub process_remote_command { if ( ! $is_connected++ ){ say "making connection"; $project->{remote_control_socket} = $project->{remote_control_socket}->accept(); $this_engine->{events}->{remote_control} = AE::io( $project->{remote_control_socket}, 0, \&process_remote_com +mand ); } my $input; eval { $project->{remote_control_socket}->recv($input, $project->{rem +ote_control_socket}->sockopt(SO_RCVBUF)); }; $@ and say("caught error: $@"), reset_socket(), return; logpkg('debug',"Got remote control socketput: $input"); eval { $project->{remote_control_socket}->send(process_command($input +)); }; $@ and say("caught error: $@"), reset_socket(), return; } sub reset_socket { undef $is_connected; undef $@; $project->{remote_control_socket}->shutdown(2); remove_watcher(); start_remote(); } sub logpkg { say join " ", @_ } sub process_command { "processed: ". shift } # client script use Modern::Perl; use IO::Socket::INET; my $tcp = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => +'57000', Proto => 'tcp', Type => SOCK_STREAM) || die $!; my $cmd = 'eval $this_track->name'; do_remote_cmd($cmd); do_remote_cmd("greetings"); do_remote_cmd("earthlings"); do_remote_cmd("take me...."); $tcp->shutdown(2); sub do_remote_cmd { my $cmd = shift; $tcp->send($cmd); $tcp->recv(my $result, 65536); say "command: $cmd, result: $result"; }

In reply to Re: Socket error: Cannot determine peer address (solved!) by gnosti
in thread Socket error: Cannot determine peer address (solved!) by gnosti

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.