Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

After looking into POE (which is lovely and clever), I've decided to use SocketFactory for my tcp server needs. This is great until I get into some error handling. I usually set alarms, which do some clean up, and then close the socket. I close the socket to get the connection to drop, thus allowing the client to drop off.

Normally, I just use close(). But I need the socket name for that, and I have no idea how to find the socket that I'm using inside a session.

I tried using getsockname with no success. Any monks out there know either how to close a particular socket, or where the HEAP keys are documented?

Here's a sample of what I'm trying to do. Also, my clients don't have access to ALARM (Winders), so the drop has to happen on my end.

#!/usr/bin/perl use POE qw( Wheel::SocketFactory Wheel::ReadWrite Filter::Line Driver::SysRW ); new POE::Session ( _start => \&collector, agent_handler=>\&agent_handler, accept_failed=>\&accept_failed, agent_input=>\&agent_input, agent_error=>\&agent_error ); $poe_kernel->run(); exit; # Session Sub Defs sub collector { $_[HEAP]->{listener} = new POE::Wheel::SocketFactory ( BindPort => PORT, Reuse => 'yes', SuccessEvent => 'agent_handler', FailureEvent => 'accept_failed' ); print "SERVER: Started Collector on port ",PORT,"\n"; } # Non-Session Sub Defs sub accept_failed { my ($function, $error) = @_[ARG0, ARG2]; delete $_[HEAP]->{listener}; print "SERVER: call to $function() failed: $error.\n"; } sub agent_handler { print "Agent handler called\n"; my ($heap, $socket) = @_[HEAP, ARG0]; $heap->{readwrite} = new POE::Wheel::ReadWrite ( Handle => $socket, Driver => new POE::Driver::SysRW (), Filter => new POE::Filter::Line (), InputEvent => agent_input, ErrorEvent => agent_error, ); $heap->{peerhost} = inet_ntoa($_[ARG1]); print "CHILD: Connected to $heap->{peerhost}.\n"; } sub agent_input { $heap=$_[HEAP]; print "agent_input called\n"; $input_sysid=$_[ARG0]; print "input system id is $input_sysid from $heap->{peerhost}\n"; print "Sending ack back\n"; $heap->{readwrite}->put( "Hello, client!" ); $agent_alarm = $poe_kernel->delay_set( agent_error, 5 ); } sub agent_error { print "Got an agent error, exiting agent session!\n"; my ($function, $error) = @_[ARG0, ARG2]; print "SERVER: call to $function() failed: $error.\n"; # I sure would like to close the socket here... }


Declarent


In reply to How do I close a POE SocketFactory Socket? by Declarent

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 avoiding work at the Monastery: (4)
As of 2024-04-16 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found