Perl套接字编程发送方法

时间:2019-02-07 06:31:19

标签: perl sockets

我现在正在为简单的通讯进行套接字编程。

我注意到服务器不是我创建的服务器,并且运行良好(考虑到实验性客户端)

在我的代码中,recv可以正常工作,但是send不起作用。我的代码有什么问题吗?

my $socket = new IO::Socket::INET (
    #PeerHost => '127.0.0.1',
    PeerHost => '192.168.0.100',
    PeerPort => '8472',
    Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

while (1) {
    my $response = "";
    $socket->recv($response, 1024);
    if ($response) {
        my @test = split(//,$response);
        my ($length,$data) = unpack("N A*",$response);
        %json = json_decode($data,$length);

        switch ($json{'type'}) {
            case 1 { print "Game Start\n";}
            #case 2 { my $tmp = &my_turn(%json);} #my_turn func is return  "{'type': 0, 'point': [5, 4]}", but fail!
            #case 2 { $socket->send("{'type': 0, 'point': [5, 4]}");} # fail!
            case 2 { print $socket "{'type': 0, 'point': [5, 4]}"; print "ok\n";} # print is executed. However, the server does not receive packets
            #case 2 { $socket->send("{'type': 0, 'point': [5, 4]}");} #fail...
            case 3 { print "ACCEPT\n";}
            case 5 { print "NOPOINT\n";}
            case 6 { print "GAMEOVER\n";}
            case 7 { print "ERROR\n";}
            else {print "ERROR type : $json{'type'}\n"}
        }
    }
}

服务器正常运行。我检查了服务器提供的示例源(Python代码)。我想念什么?

1 个答案:

答案 0 :(得分:2)

  • 您不能假设recv(或read)将返回整个响应。您需要反复调用它。
  • 您不能假设recv(或read)只是响应。您需要限制读取的缓冲区大小。
  • decode_json返回一个引用(不是您可以分配给哈希的键/值对列表)。
  • 您可能还需要处理JSON字符串的编码。下面的示例假定使用UTF-8编码。
  • 对服务器的JSON响应(原始代码中的case 2)也需要包含长度。

应改为使用以下代码:

#!/usr/bin/perl
use strict;
use warnings;

use JSON;
use IO::Socket;

my $socket = IO::Socket::INET->new(
    PeerHost => '127.0.0.1',
    PeerPort => '22',
    Proto    => 'tcp',
) or
    die "cannot connect to the server $!\n";

print "connected to the server\n";

sub read_bytes($$) {
    my($socket, $length) = @_;
    my $result = '';
    print "ATTEMPT TO READ ${length}\n";
    while ($length > 0) {
        my $received = $socket->read($result, $length, length($result));
        die "socket error: $!\n" unless defined($received);
        die "unexpected EOF\n"   unless $received;
        $length -= $received;
    }

    print "READ '${result}'\n";
    return($result);
}

while (1) {
    my $length = unpack("N", read_bytes($socket, 4));
    my $json   = read_bytes($socket, $length);
    my $data   = JSON->new->utf8->decode($json);

    print $data->{type}, "\n";

    if ($data->{type} == 2) {
        my $response = {
            type  => 0,
            point => [5, 4],
        };
        my $resp_json = JSON->new->utf8->encode($response);
        print "JSON:   ${resp_json}\n";

        my $packet = pack('NA*', length($resp_json), $resp_json);
        print "PACKET: ", unpack('H*', $packet), "\n";

        $socket->write($packet);
    }
}

由于我无权访问您的服务器,因此在本地计算机上使用了sshd,这当然不会向我发送JSON。但这表明阅读有效:-)

$ perl dummy.pl
connected to the server
ATTEMPT TO READ 4
READ 'SSH-'
ATTEMPT TO READ 1397966893
^C

对服务器的示例响应输出为:

JSON:   {"type":0,"point":[5,4]}
PACKET: 000000187b2274797065223a302c22706f696e74223a5b352c345d7d