gpt4 book ai didi

java - Perl 客户端到 Java 服务器

转载 作者:行者123 更新时间:2023-12-03 01:09:31 26 4
gpt4 key购买 nike

我正在尝试编写一个 Perl 客户端程序来连接到 Java 服务器应用程序 (JDuplicate)。我看到 java 服务器使用 DataInput.readUTF 和 DataInput.writeUTF 方法,JDuplicate 网站将其列为“Java 的修改版 UTF-8 协议(protocol)”。

我的测试程序非常简单,我正在尝试发送客户端类型数据,这应该调用服务器的响应,但它只是超时:

#!/usr/bin/perluse strict;use Encode;use IO::Socket;my $remote = IO::Socket::INET->new(  Proto => 'tcp',  PeerAddr => 'localhost',  PeerPort => '10421') or die "Cannot connect to server\n";$|++;$remote->send(encode_utf8("CLIENTTYPE|JDSC#0.5.9#0.2"));while (<$remote>) {  print $_,"\n";}close($remote);exit(0);

我尝试过 $remote->send(pack("U","..."));,我尝试过“use utf8;”,我尝试过 binmode($remote, ":utf8 "),并且我尝试仅发送纯 ASCII 文本,但没有得到任何响应。

我可以看到使用 tcpdump 发送的数据,全部在一个数据包中,但服务器本身不对其执行任何操作(除了确认数据包)。

我需要做一些额外的事情来满足Java的“修改的”utf实现吗?

谢谢。

最佳答案

您必须实现 protocol正确的是:

First, the total number of bytes needed to represent all the characters of s is calculated. If this number is larger than 65535, then a UTFDataFormatException is thrown. Otherwise, this length is written to the output stream in exactly the manner of the writeShort method; after this, the one-, two-, or three-byte representation of each character in the string s is written.

writeShort 文档中所示,它按网络顺序发送 16 位数量。

在 Perl 中,这类似于

sub sendmsg {
my($s,$msg) = @_;

die "message too long" if length($msg) > 0xffff;

my $sent = $s->send(
pack(n => (length($msg) & 0xffff)) .
$msg
);

die "send: $!" unless defined $sent;
die "short write" unless $sent == length($msg) + 2;
}

sub readmsg {
my($s) = @_;
my $buf;
my $nread;

$nread = $s->read($buf, 2);
die "read: $!" unless defined $nread;
die "short read" unless $nread == 2;

my $len = unpack n => $buf;

$nread = $s->read($buf, $len);
die "read: $!" unless defined $nread;
die "short read" unless $nread == $len;

$buf;
}

尽管上面的代码不执行修改后的 UTF 编码,但它会引发响应:

my $remote = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerPort => '10421'
) or die "Cannot connect to server: $@\n";

my $msg = "CLIENTTYPE|JDSC#0.5.9#0.2";

sendmsg $remote, $msg;

my $buf = readmsg $remote;
print "[$buf]\n";

输出:

[SERVERTYPE|JDuplicate#0.5.9 beta (build 584)#0.2]

关于java - Perl 客户端到 Java 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2052837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com