我的Linux Perl脚本有问题.它的主要目的是成为3个应用程序之间的中间人.该怎么做:
它应该能够等待UDP文本(没有空格) $udp_port
当它收到该UDP文本时,它应该将它转发到连接的TCP客户端
问题是我的应用程序当前工作,直到我第一次断开TCP客户端.然后我再也无法连接它了,它在接收到下一个UDP数据包后超时$udp_port
.因此,基本上每当我想重新连接TCP时,我都必须重新启动应用程序.
所有这一切都应该尽可能快(每毫秒计数).发送到UDP或TCP的文本不包含空格.没有必要同时支持多个TCP客户端,但它肯定会有利:-)
这是我目前的代码:
#!/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"; }
这是我在没有线程的情况下提出几条建议后尝试的代码.问题是你甚至可以连接TCP客户端消息"尝试设置UDP \n永远不会显示.可能是我做错了.tcp客户端只是连接并等待服务器发送一些数据.Udp到达但它是不接受.这是代码:
#!/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"; }
Anonymous.. 8
断开连接后,您可能希望循环并->accept
再次等待新连接.
了解use strict;
和use warnings;
发现任何错误也是一个好主意.
编辑:我认为glob
无论你认为它做什么都没有.
断开连接后,您可能希望循环并->accept
再次等待新连接.
了解use strict;
和use warnings;
发现任何错误也是一个好主意.
编辑:我认为glob
无论你认为它做什么都没有.
尝试将您的代码归结为最简单的程序,该程序接受TCP连接,断开连接,然后接受另一个.当你走得那么远时,其他一切都只是在细化细节.
匿名者的暗示很响亮.你在问题中包含的代码中有太多的小错误,所以你最好从一个简单的案例开始,然后再建立它.
一个简单的TCP侦听器可能看起来像这样 - 它只是侦听localhost上的一个端口并打印它看到的内容:
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"; }