I have this little script that does the job pretty well, but sometimes it fails. This fails in 2 cases:
with error message: Cannot determine peer address at ./tcp-new.pl line 52
without output or anything else, it simply cannot deliver what it received to the connected Tcp Client. This usually happens after I disconnect from the server, go home and reconnect it. To fix this restart is required, and it starts to work. Sometimes this problem is followed by the problem mentioned in paragraph 1.
Note: this is not a problem when I disconnect and reconnect to it for a short period of time (unless error number 1 occurred).
So can someone help me make this code a little more stable, so I donβt need 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); } } } }
source share