I have a problem with Perl script for Linux. It's main purpose is to be middleman between 3 applications. What it should do:
- It should be able to wait for UDP text (without spaces) on
$udp_port
- When it receives that UDP text it should forward it to the TCP client that is connected
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 $udp_port
. So basically whenever I want to reconnect with TCP I have to restart app.
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 :-)
Here's my current 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->new( Proto => 'tcp', LocalPort => $tcp_port, Listen => SOMAXCONN,Reuse => 1)|| die @!;
my $udp_sock = new IO::Socket::INET(LocalPort => $udp_port, Proto => "udp") || die @!;
my (@threads);
print "[$title]\n";
sub mySubTcp($)
{
my ($popup) = @_;
print "[TCP][CLIENT CONNECTED]\n";
while (my $answer = <$popup>)
{
chomp $answer;
my ($pass, $announce) = split ' ', $answer;
print $answer . '\n';
}
printf "[TCP][CLIENT DISCONNECTED]\n";
}
my $client = $tcp_sock->accept();
$client->autoflush(1);
my $thr = threads->new(\&mySubTcp, $client);
while ($udp_sock->recv(my $buf, 1024))
{
chomp $buf;
my $announce = $buf;
print "[ANNOUNCE] $announce [START]\n";
print $client $announce . "\n";
print "[ANNOUNCE] $announce [END]\n";
}
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:
#!/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->new( Proto => 'tcp', LocalPort => $tcp_port, Listen => SOMAXCONN,Reuse => 1)|| die @!;
my (@threads);
print "[$title]\n";
for (;;)
{
my $open_socket = $tcp_sock->accept();
print "[TCP][CLIENT CONNECTED]\n";
while (my $input = <$open_socket>)
{
print "Trying to setup UDP\n";
my $udp_sock = new IO::Socket::INET(LocalPort => $udp_port, Proto => "udp") || die @!;
while ($udp_sock->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";
}