title: How can I accept multiple TCP connections in Perl?
body: <p>I have a problem with Perl script for Linux. It's main purpose is to be middleman between 3 applications. What it should do:</p>
  
  <ol>
  <li>It should be able to wait for UDP text (without spaces) on <code>$udp_port</code>  </li>
  <li>When it receives that UDP text it should forward it to the TCP client that is connected</li>
  </ol>
  
  <p>Problem is my app currently works until the first time I disconnect with TCP client. Then I cannot connect to it any longer, and it times out after it receives next UDP packet on <code>$udp_port</code>. So basically whenever I want to reconnect with TCP I have to restart app. </p>
  
  <p>All of this should be as fast as possible (every millisecond counts). The text sent to UDP or TCP doesn't contain spaces. It's not necessary to be able to support multiple TCP clients at once, but it would certainly be advantage :-)</p>
  
  <p>Here's my current code:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  use IO::Socket;
  use Net::hostent;
  use threads;
  use threads::shared;
  
  my $tcp_port = "10008";  # connection from TCP Client
  my $udp_port = "2099";  # connection from Announcer
  my $udp_password = ""; # password from Announcer
  my $title = "Middle Man server version 0.1";
  my $tcp_sock = IO::Socket::INET-&gt;new( Proto =&gt; 'tcp', LocalPort =&gt; $tcp_port, Listen =&gt; SOMAXCONN,Reuse =&gt; 1)|| die @!;
  my $udp_sock = new IO::Socket::INET(LocalPort =&gt; $udp_port, Proto =&gt; "udp") || die @!;
  
  my (@threads);
  
  print "[$title]\n";
  
  sub mySubTcp($)
  {
    my ($popup) = @_;
  
    print "[TCP][CLIENT CONNECTED]\n";
    while (my $answer = &lt;$popup&gt;)
    {
  chomp $answer;
  my ($pass, $announce) = split ' ', $answer;
  print $answer . '\n';
    }
    printf "[TCP][CLIENT DISCONNECTED]\n";
  }
  
  my $client = $tcp_sock-&gt;accept();
  $client-&gt;autoflush(1);
  
  
  my $thr = threads-&gt;new(\&amp;mySubTcp, $client);
  
  
  while ($udp_sock-&gt;recv(my $buf, 1024))
  {
    chomp $buf;
  
    my $announce = $buf;
      print "[ANNOUNCE] $announce [START]\n";
      print $client $announce . "\n";
      print "[ANNOUNCE] $announce [END]\n";
  
  }
  </code></pre>
  
  <p>Here's the code i tried after couple of suggestions to go without threading. Problem is even thou i am able to connect with TCP Client  msg "Trying to setup UDP\n is never displayed. Probably something i'm doing wrong. The tcp client just connects and waits for server to send some data. Udp arrives but it's not accepted. Here's the code:</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  use IO::Socket;
  use Net::hostent;
  use threads;
  use threads::shared;
  
  my $tcp_port = "10008";  # connection from Tcp
  my $udp_port = "2099";  # connection from Announcer
  
  my $title = "Middle Man server version 0.2";
  my $tcp_sock = IO::Socket::INET-&gt;new( Proto =&gt; 'tcp', LocalPort =&gt; $tcp_port, Listen =&gt; SOMAXCONN,Reuse =&gt; 1)|| die @!;
  
  my (@threads);
  
  print "[$title]\n";
  
  for (;;)
  {
      my $open_socket = $tcp_sock-&gt;accept();
      print "[TCP][CLIENT CONNECTED]\n";
      while (my $input = &lt;$open_socket&gt;)
      {
      print "Trying to setup UDP\n";
      my $udp_sock = new IO::Socket::INET(LocalPort =&gt; $udp_port, Proto =&gt; "udp") || die @!;
      while ($udp_sock-&gt;recv(my $buf, 1024)) {
            chomp $buf;
            print "\[ANNOUNCER\] $buf \[START\]\n";
            print $open_socket $buf . "\n";
            print "\[ANNOUNCER\] $buf \[END\]\n";
      }
      print "Closing UDP\n";
      close $udp_sock;
      #chomp $input;
      #print $input;
  }
  
      close $open_socket;
      printf "[TCP][CLIENT DISCONNECTED]\n";
  }
  </code></pre>