Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Socket error: Cannot determine peer address (solved!)

by gnosti (Chaplain)
on Dec 31, 2013 at 22:49 UTC ( [id://1068835]=perlquestion: print w/replies, xml ) Need Help??

gnosti has asked for the wisdom of the Perl Monks concerning the following question:

My script fails after one successful send/receive with "cannot determine peer address". I found a a stackoverflow post which suggests that something could be wrong with the args used (in that case, the problem was an illegal value, 255.255.255.255, as peer address).

Any ideas where the problem could be?

#server script use Modern::Perl; use EV; use AnyEvent; use IO::Socket::INET; 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"; my $in = $project->{remote_control_socket} = IO::Socket::INET->new +( qw(LocalAddr localhost LocalPort), $port, qw(Proto tcp Type), SOCK_ +STREAM, qw(Listen 1 Reuse 1) ) || die $!; $this_engine->{events}->{remote_control} = AE::io( $in, 0, \&proce +ss_remote_command ) } sub process_remote_command { my $in = $project->{remote_control_socket}; my $socket = $in->accept; $socket->recv(my $input, $in->sockopt(SO_RCVBUF)); logpkg('debug',"Got remote control input: $input"); $socket->send(process_command($input)); } 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"); + + sub do_remote_cmd { my $cmd = shift; $tcp->send($cmd); sleep 1; $tcp->recv(my $result, 65536); say "command: $cmd, result: $result"; }

Replies are listed 'Best First'.
Re: Socket error: Cannot determine peer address ($in)
by tye (Sage) on Dec 31, 2013 at 23:31 UTC

    The server processes one command per connection (after which $in gets destroyed). So the client can't send a second command w/o reconnecting (which is done in ->new() when you specify a PeerAddr).

    - tye        

Re: Socket error: Cannot determine peer address
by taint (Chaplain) on Jan 01, 2014 at 04:35 UTC
    Greetings, gnosti.

    While tye's reply is (of course) spot on. I'd also like to add that your 255.255.255.255 mask, is quite legitimate (valid). So you need not be concerned about that.

    Happy New Year! :)

    --Chris

    ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Re: Socket error: Cannot determine peer address (solved!)
by gnosti (Chaplain) on Jan 01, 2014 at 08:11 UTC
    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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1068835]
Approved by rnewsham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found