如何与Perl / Tk一起使用AnyEvent :: JSONRPC

时间:2014-05-23 15:40:10

标签: perl client-server tk json-rpc

我正在尝试使用AnyEvent::JSONRPC::TCP::ServerPerl/Tk应用程序中运行服务器,我可以从AnyEvent::JSONRPC::TCP::Client发送请求。我已经将我想要做的事情融合到这两个简单的服务器/客户端脚本中:

服务器:

use AnyEvent::JSONRPC::TCP::Server;

my $server = AnyEvent::JSONRPC::TCP::Server->new( port => 4123 );
$server->reg_cb(
    echo => sub {
        my ($res_cv, @params) = @_;
        print STDERR "echo() called with params ".join(",",@params)."\n";
        $res_cv->result(@params);
    }
);

use Tk;
my $mw = MainWindow->new(-title => "Server is running");
my $ex = $mw->Button(-text => "   Exit the Server   ",
                     -command => sub {exit;})->pack();
MainLoop;

和相应的客户:

use AnyEvent::JSONRPC::TCP::Client;

my $client = AnyEvent::JSONRPC::TCP::Client->new(
    host => '127.0.0.1',
    port => 4123,
);

use Tk;
my $mw = MainWindow->new(-title => "Client is running");
my $ec = $mw->Button(-text => "   Call Echo   ", -command =>
    sub {
      my $res = $client->call( echo => 'foo bar' )->recv();
      print STDERR "Called echo() remotely, got back '$res'\n";
    }
  )->pack();

my $ex = $mw->Button(-text => "   Exit the Client   ",
                     -command => sub {exit;})->pack();
MainLoop;

在我的Win32环境中,我首先从一个命令shell启动服务器,它给了我主窗口,然后我从另一个命令shell启动客户端,这似乎也可以通过另一个窗口启动。

问题是:客户端无法连接到服务器。几秒后它说:

Tk::Error: Client got error: Failed to connect 127.0.0.1:4123: A connection attempt
    failed because  the connected party did not properly respond after a period of
    time, or established connection failed because connected host has failed to
    respond. at C:/Perl5.16/site/lib/AnyEvent/Socket.pm line 998.
 [\&AnyEvent::Impl::Tk::__ANON__]
 ("after" script)

当我在与客户端相同的机器上启动服务器时,我认为客户端代码中使用的“localhost”IP地址127.0.0.1应该找到服务器,对吧? 由于我不是很深入网络编程,我迷失在这里。

有没有人有想法,我怎么开始调试这个?或者有人甚至立刻知道我做错了什么。

顺便说一句:我在Windows 7机器上运行ActiveState Perl 5.16.3。我想使用JSON RPC,因为建议在与Perl接口时使用基于Python的解决方案。

谢谢!

0 个答案:

没有答案