How can I accept multiple TCP connections in Perl? How can I accept multiple TCP connections in Perl? linux linux

How can I accept multiple TCP connections in Perl?


After it disconnects, you'll probably want to loop around and wait for a new connection with ->accept again.

It would also be a good idea to use strict; and use warnings; to ferret out any errors.

Edit: And I don't think glob does whatever you think it does there.


Try to boil down your code into the simplest possible program that accepts a TCP connection, disconnects, then accepts another one. When you've gotten that far, everything else is just refining the details.

Anonymous's hints were bang on. You've got way too many little errors in the code you included in your question, so you'd be better off starting over with a simple case and then building it up.

A simple TCP listener might look something like this -- it simply listens on a port on localhost and prints what it sees:

use strict; use warnings;use IO::Socket;my $socket = IO::Socket::INET->new(    LocalHost => 'localhost',    LocalPort => '5555',    Proto => 'tcp',    Listen => 1,    Reuse => 1,) or die "Could not create socket: $!";for (;;){    my $open_socket = $socket->accept();    print "Got a connection!\n";    while (my $input = <$open_socket>)    {        print $input;    }    close $open_socket;    print "Connection closed.\n\n";}


It's not threaded, but I think this does what I think you want:

#!/usr/bin/perluse 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,                                       LocalAddr => 'localhost',                                       LocalPort => $tcp_port,                                       Proto     => 'tcp',                                       ReuseAddr => 1,                                      );my $udp_socket = IO::Socket::INET->new(                                       LocalAddr => 'localhost',                                       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);## Loop forever, reading data from the UDP socket and writing it to the## TCP socket(s).  Might want to install some kind of signal handler to## ensure a clean shutdown.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 are             ## stateless (no accept necessary)...            my $new_tcp = $read->accept();            $write_select->add($new_tcp);        }        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.  What             ## happens if no TCP clients are connected?  Will IO::Select throw some            ## kind of error trying to select on an empty set of sockets, or will the            ## data read from UDP just get dropped on the floor?              my @write = $write_select->can_write();             foreach my $write (@write) {                ## Make sure the socket is still connected before writing.  Do we also                ## need a SIGPIPE handler somewhere?                if ($write->connected()) {                    $write->send($recv_buffer);                }                else {                    $write_select->remove($write);                }            }        }    }}

Disclaimer: I just banged that out. I imagine it's very fragile. Don't try and use that in a production environment without much testing and bulletproofing. It might eat your data. It might try and eat your lunch. Use at your own risk. No warranty.