I'm trying to duplicate something similar to the port forwarding examples in the cookbook except with one difference...I want the TCP client to connect first and if successful, the TCP server will then starts listening for remote connections. This is opposite of the examples where the TCP server starts listening first, then the TCP client connects.

I've read the different cookbook examples trying to get a hold on how to do this but I can't figure out how to send a "shutdown" to the TCP server when the TCP client gets disconnected. Below is a very basic example. This is my first time delving into POE so any pointers are helpful.

#!/usr/bin/perl use warnings; use strict; use POE qw(Component::Server::TCP Component::Client::TCP); my $forward_address = "localhost"; my $forward_port = 7500; my $remote_port = 7400; POE::Session->create ( inline_states => { _start => \&forwarder_start, listener_start => \&spawn_client_listener, } ); $poe_kernel->run(); exit 0; sub forwarder_start { POE::Component::Client::TCP->new( Alias => 'ForwardConnection', RemoteAddress => $forward_address, RemotePort => $forward_port, Connected => \&forward_server_connected, ConnectError => \&forward_server_reconnect, ServerInput => \&forward_client_input, Disconnected => \&forward_disconnected, ); } sub spawn_client_listener { POE::Component::Server::TCP->new( Alias => 'RemoteListener', Port => $remote_port, ClientInput => \&remote_client_input, InlineStates => { remote_shutdown => \&remote_shutdown, } ); } sub forward_server_connected { $_[KERNEL]->yield('listener_start'); } sub forward_server_reconnect { print "not connected\n"; $_[KERNEL]->delay(_start => 15); } sub forward_client_input { } sub forward_disconnected { $_[KERNEL]->post(RemoteListener => "remote_shutdown"); $_[KERNEL]->yield("_start"); } sub remote_client_input { } sub remote_shutdown { print "shutting down\n"; $_[KERNEL]->yield("shutdown"); }

Thanks, Jason

In reply to POE - can't shutdown Component::Server::TCP from Component::Client::TCP by jsarrel

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.