Java Socket 客户端和 C++ 之间的网络(基于Boost)服务器

发布于 11-29 06:34 字数 1126 浏览 2 评论 0原文

在工作中,我正在设计一个用于控制机器人组的用户界面。这些机器人使用 UDP 广播来管理彼此之间的移动。

GUI 需要能够与机器人进行通信。为此,运行中间服务器。所有机器人都会监听它(使用 UDP 传感器),所有正在运行的 GUI 都会连接到它(通过 TCP)。它管理 GUI <->机器人通讯。

然而,服务器是用 C++ Boost 库编写的,GUI 是用 Java 编写的,并且出现了一些网络问题。我相当容易地使用套接字连接到服务器:

try {
    socket = new Socket(targetAddress, targetPort);
} catch (IOException e) { e.printStackTrace(); }

服务器注册连接,一切看起来都很好。

但是,当我尝试发送字符串时:

try {
    stream.writeUTF(message);
    stream.flush();
} catch (IOException e) { e.printStackTrace(); }

注意:我最初使用 PrintWriter 一次发送一个字符串 (println()),但切换到 DataOutputStream 看看是否有帮助。

问题。 Boost 甚至没有注册我发送了消息,尽管 Java 成功注册了。此外,当从服务器发送字符串时,它们的格式无法识别。

对这个问题的一些研究表明,Boost 会自动将“标头”文本附加到它发送的所有消息中,帮助它归档收到的消息。由于 Java 不这样做,看来这可能就是原因。这是正确的吗?如果是这样,我们该如何绕过它?


一些注意事项:

  • 由于时间限制,大规模切换到不同的库/语言实际上并不是一个选择。但是,如果有一个 C++ TCP 库允许服务器接收我发送的消息,并且我们可以轻松集成它,那就完美了。
  • 连接到 Java 服务器时,Java 网络代码可以完美运行。困难似乎发生在 Boost-Java 接口中。
  • 不幸的是,我自己或从事该项目这方面工作的其他人都没有网络方面的经验。 :( 我的经验是 Java 和 GUI 开发,另一个人是人工智能程序员/硬件专家。对此问题的任何和所有帮助都将非常受欢迎。

At work I'm designing a user interface for controlling groups of robots. The robots use UDP broadcasts to manage their movements with one another.

The GUI needs to be able to communicate to the robots. To this end, an intermediary server is run. All robots listen to it (with UDP sensors), and all running GUIs connect to it (via TCP). It manages GUI <-> Robot communications.

However, the server is written with the C++ Boost library, and the GUI is written in Java, and some issues with the networking are occurring. I connect to the server with a socket fairly easily:

try {
    socket = new Socket(targetAddress, targetPort);
} catch (IOException e) { e.printStackTrace(); }

The server registers the connection and everything looks good.

However, when I try to send Strings:

try {
    stream.writeUTF(message);
    stream.flush();
} catch (IOException e) { e.printStackTrace(); }

Note: I was initially using a PrintWriter to send strings one at a time (println()) but switched to DataOutputStream to see if it would help.

We run into problems. Boost does not even register that I sent the message, even though Java successfully did. Additionally, when Strings are sent from the server, they are in an unrecognizable format.

A bit of looking into the problem suggests that Boost automatically appends "header" text to all messages it sends, helping it to archive messages it receives. Since Java doesn't do this, it seems like this may be the cause. Is this correct? If so, how can we get around it?


A few notes:

  • Due to time constraints, switching to different libraries/languages on a large scale is not really an option. However, if there is a C++ TCP library that will allow the server to receive the messages I send, and we can easily integrate it, that would be perfect.
  • The Java networking code works perfectly when connecting to a Java server. The difficulties seem to be happening in the Boost-Java interface.
  • Unfortunately, neither myself or the other person working on this aspect of the project are that experienced in networking. :( My experience is with Java and GUI development, and the other person is an AI programmer / hardware specialist. Any and all help with this issue would be incredibly welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙2024-12-06 06:34:46

从发送开始接收第一个字节,然后接收整数。请注意大小(C++ 中为 64 位整数)和字节顺序(最低位优先与最高位优先)。正如 Sam Miller 提到的,您需要创建自己的协议,该协议将定义您可以发送和发送的消息类型。它们的元素是如何排序的。

可能发生的情况是 Boost 首先发送字符串的长度。请仔细阅读使用 Boost 库发送的消息的有线格式以及 DataOutputStream 使用的有线格式。

Start with sending & receiving first bytes, then ints. Be aware of sizing (f.e. 64 bit ints in C++) and byte ordering (least vs most significant first). As Sam Miller mentioned, you will need to create your own protocol, that will define what kind of messages you can send & how their elements are ordered.

What probably happens is that Boost sends the length of the string first. Do read up on the wire format of messages sent using the Boost library, and of the wire format used by DataOutputStream.

蓝眸2024-12-06 06:34:46

首先,您需要知道 Boost 使用哪种编码、字节顺序和包格式来确定命令的开始和结束。
例如,命令可能类似于 WALK,但您根本不会通过连接发送 WALK 字符串。
它可以使用像 \n 这样的分隔符,所以你可以发送:WALK\n
另一件事是要知道它使用的编码是什么,您可以使用字符串方法 myString.getBytes("UTF-8");使用套接字的输出流通过网络发送 UTF-8 格式的 byte[]。

First of all you need to know which encoding, byte order and package format Boost is using to determine the start and the end of a comand.
For example, a comand could be like WALK, but you simply will not send a WALK string over the connection.
It can be using a delimiter like \n so you would send: WALK\n
another thing, is to know what the encoding is it using, you can use the string method myString.getBytes("UTF-8"); to send the byte[] in UTF-8 over the network using socket´s outputstream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文