创建一个软件路由器

时间:2018-09-03 03:59:05

标签: perl tk inet

我想构建一个软件路由器。

我已经编写了打开套接字,接收数据并打印其接收到的数据的代码。该GUI将使用     Tk

下面是该代码的简单版本,除了不分叉new_port子例程外,它基本上完成了我想做的事情。每次我单击提交按钮,Tk窗口都会卡住。如果有人可以帮助您将派生添加到new_port子例程中,那么它将产生一个新的子进程。我的想法是我可以填写表格中的新端口并命中sumbit。窗口关闭,然后我再按一次new,将新端口插入其中,现在第二个插座与第一个同时打开。即同时监听I.E端口1234和5678。

#!/usr/bin/perl -w
 use IO::Socket::INET;
 use Tk;

$myip = `ifconfig | grep -i inet | head -1 | cut -d ":" -f2 | cut -d " " -f1`;

sub new_port {
    my $socket = new IO::Socket::INET ( 
        LocalHost => "$myip",
        LocalPort => "$myport",
        Proto => 'tcp'
        Reuse => 1);
die "Cannot create socket on local host" unless $socket;
print "Server waiting for client connection on port $myport\n";

while(1)
{
 my $client_socket = $socket->accept();
 my $client_address = $client_socket->peerhost();
 my $client_port = $client_socket->peerport();
 my $input_data = "";
 my $received_data = "";
 do
 {
   $client_socket->recv($received_data, 65536);
   $input_data = $input_data . $received_data;
 } while ($received_data ne "");
 print "INPUT----------------------------------\n";
 print "Data from $client_address on port $client_port\n";
 print $input_data;
 shutdown($client_socket, 1);
  }
 }

 sub new_port_window {
   my $sw = MainWindow->new;
   $sw->geometry("200x100");
   $sw->title("port opener");
   $sw->Label(-text "Insert port #")->place(-anchor => 'center', -relx => 0.5, -rely => 0.2);
   $sw->Entry(-bg => 'white', -fg => 'black', -textvariable => \$myport)->place(-anchor => 'center', -relx => 0.5, -rely => 0.4);
   $sw->Button(-text "submit", -command => sub{new_port})->place(width => 100,
   -anchor => "center",
   -relx => 0.5,
   -rely => 0.8);
}

 my $mw = MainWindow->new;
       $mw->geometry("150x100");
       $mw->title("GUI TEST NEW FUNCTION");
       $mw->Label(-text => "click new")->place(-anchor => "center", -relx => 0.5, -rely => 0.3);
       $mw->Button(-text => "NEW", -command =>sub{new_port_window})->place(-width => 50, -anchor => "center", -relx => 0.5, -rely => 0.8);
MainLoop;

1 个答案:

答案 0 :(得分:0)

因此,我能够通过大量实验来使其正​​常工作。诀窍是派生子例程并将pid存储在文件中,以供以后用于终止进程,该进程由于处于无限循环而永远不会退出。

Sub forker {
     $pid = fork ();
      if ( $pid == 0 ) {
          new_port();
      }
      my $pid_file = 'router.pid';
      open (my $pidlog, '>>', $pid_file) or die "cannot open file";
       print $pidlog "$pid\n";
       close $pidlog;
  }