develooper Front page | perl.beginners | Postings from April 2010

Perl not closing TCP sockets if clients are no longer connected?

Thread Next
Leon Meyer
April 28, 2010 12:29
Perl not closing TCP sockets if clients are no longer connected?
Message ID:
The purpose of the application is to listen for a specific UDP multicast and
then to forward the data to any TCP clients connected to the server. The
code works fine, but I have a problem with the sockets not closing after the
TCP clients disconnects. A socketsniffer utility shows the the sockets
remain open and all the UDP data continues to be forwarded to the clients.
The problem I believe is with the "if ($write->connected())" block as it
always return true, even if the TCP client is no longer connected. I use
standard Windows Telnet to connect to the server and to see the data. When I
close telnet, the TCP socket is suppose to close on the server.

Any reason why connected() show the connections as active even if they are
not? Also, what alternative should I use then?



use IO::Socket::Multicast;
use IO::Socket;
use IO::Select;

my $tcp_port = "4550";
my $tcp_socket = IO::Socket::INET->new(
                                       Listen    => SOMAXCONN,
                                       LocalAddr => '',
                                       LocalPort => $tcp_port,
                                       Proto     => 'tcp',
                                       ReuseAddr => 1,
setsockopt( $tcp_socket, IPPROTO_TCP, TCP_NODELAY, 1);

use constant GROUP => '';
use constant PORT  => '6550';
my $udp_socket= IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>PORT);
$udp_socket->mcast_add(GROUP) || die "Couldn't set group: $!\n";

my $read_select  = IO::Select->new();
my $write_select = IO::Select->new();


## Loop forever, reading data from the UDP socket and writing it to the
## TCP socket(s).
while (1) {

    ## No timeout specified (see docs for IO::Select).  This will block
until a TCP
    ## client connects or we have data.
    my @read = $read_select->can_read();

    foreach my $read (@read) {

        if ($read == $tcp_socket) {

            ## Handle connect from TCP client.  Note that UDP connections
            ## stateless (no accept necessary)...
            my $new_tcp = $read->accept();

        elsif ($read == $udp_socket) {

            ## Handle data received from UDP socket...
            my $recv_buffer;

            $udp_socket->recv($recv_buffer, 1024, undef);

            ## Write the data read from UDP out to the TCP client(s).
Again, no
            ## timeout.  This will block until a TCP socket is writable.
            my @write = $write_select->can_write();

            foreach my $write (@write) {

                ## Make sure the socket is still connected before writing.
           if ($write->connected()) {
                else {
                    close $write;






Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About