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"); }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |