I have a problem with a Perl script for Linux. The main goal is to mediate between the three applications. What should he do:
- It should be able to wait for UDP text (without spaces) on
$udp_port - When it receives this UDP text, it should send it to the TCP client that is connected
The problem is that my application runs until the first time when I disconnect from the TCP client. Then I can no longer connect to it, and it expires after receiving the next UDP packet on $udp_port . So basically, when I want to connect to TCP, I have to restart the application.
All this should be as fast as possible (every milliseconds are counted). Text sent in UDP or TCP does not contain spaces. It is not necessary to be able to support multiple TCP clients at once, but this will certainly be useful :-)
Here is 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 is the code I tried after a couple of sentences to go without threads. The problem is even that I can connect to TCP Client msg "Attempting to configure UDP \ n is never displayed. Maybe I'm doing something wrong. The tcp client just connects and waits for the server to send some data. It is not accepted. Here is 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"; }
source share