How can I diagnose "Cannot determine peer address" in my Perl TCP script?
- by MadBoy
I've this little script which does it's job pretty well but sometimes it tends to fail. It fails in 2 cases:
with error send: Cannot determine peer address at ./tcp-new.pl line 52
with no output or anything, it just fails to deliver what it got to connected Tcp Client. Usually it happens after I disconnect from server, go home and connect it again. To fix this restart is required and it starts working. Sometimes this problem is followed by problem mentioned in point 1.
Note: it's not problem when I disconnect and reconnect to it again within short amount of time (unless error nr 1 happens).
So can anyone help me make this code be a bit more stable so I don't have to restart it every day?
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use IO::Select;
my $tcp_port = "10008";
my $udp_port = "2099";
my $tcp_socket = IO::Socket::INET->new(
Listen => SOMAXCONN, LocalPort => $tcp_port,
Proto => 'tcp', ReuseAddr => 1,
);
my $udp_socket = IO::Socket::INET->new(
LocalPort => $udp_port, Proto => 'udp',
);
my $read_select = IO::Select->new();
my $write_select = IO::Select->new();
$read_select->add($tcp_socket);
$read_select->add($udp_socket);
while (1) {
my @read = $read_select->can_read();
foreach my $read (@read) {
if ($read == $tcp_socket) {
my $new_tcp = $read->accept();
$write_select->add($new_tcp);
} elsif ($read == $udp_socket) {
my $recv_buffer;
$udp_socket->recv($recv_buffer, 1024, undef);
my @write = $write_select->can_write();
foreach my $write (@write) {
$write->send($recv_buffer);
}
}
}
}