JSSE 连接上的 XML 流结束?

发布于 2024-07-08 05:13:11 字数 406 浏览 9 评论 0原文

我有一个 Java 服务器,它接受使用 JSSE 的 SSL 连接,并在流中使用简单的 XML 消息格式。 我希望服务器读取完整的消息,然后发送回复。 事实证明这非常困难,因为 org.xml.sax.XMLReader 想要读取整个流,然后调用 close()。 我知道这看起来很奇怪,但在带有 Sun JSSE 提供程序的 Java 6 中,这确实关闭了 SSLSocket 的两端,因此没有消息可以返回。 我尝试在客户端使用 Socket 的 shutdownOutput() 方法,但 JSSE 不支持此方法。

我的解决方案是传递一个包装在自定义类中的 InputStream,该类默默地忽略关闭请求,并指示流在遇到第一个空行时关闭。 这将 XML 限制在通常有效的范围之外,但如果需要,客户端可以轻松过滤掉输入中的空白行。 有更好的解决方案吗?

I have a Java server that accepts SSL connections using JSSE and uses a simple XML message format inside the stream. I would like the server to read a complete message and then send a reply. This turns out to be quite difficult because org.xml.sax.XMLReader wants to read the entire stream and then call close(). I know it seems strange, but in Java 6 with the Sun JSSE provider this really does close both ends of the SSLSocket so no message can go back. I tried using the shutdownOutput() method of Socket on the client side, but this is unsupported with JSSE.

My solution was to pass an InputStream wrapped in a custom class that silently ignores close requests and indicates that the stream is closed when it encounters the first blank line. This constrains the XML beyond what is normally valid, but the client can easily filter out blank lines in the input if necessary. Is there a better solution?

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

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

发布评论

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

评论(2

薆情海 2024-07-15 05:13:11

您的客户如何知道完整的消息已被阅读? 如果它必须读取直到输入流到达末尾,那么根据定义,您之后将无法从流中读取任何内容。 其次,在输入流上调用 close() 不应关闭套接字。 当您在输入流上调用 close() 时,您确定套接字已关闭吗? 当对等方注意到其输出流(您的输入流)已关闭时,连接可能(错误地)被对等方关闭。

避免读取直到输入流末尾的解决方案是以某种方式定界输入消息。 例如,您可以在消息之前发送消息的长度,然后将整个消息从流中读取到缓冲区中,然后使用 XMLReader 解析缓冲区,或者创建一个特殊的 InputStream,它在指定的时间后到达其末尾。数据。 或者您可以在消息后面的流中添加一个特殊的分隔符,然后您将创建一个特殊的 InputStream,当底层流生成分隔符时到达其末尾。

How does your client know when a complete message has been read? If it has to read until the input stream reaches its end, then, by definition, you won't be able to read anything from the stream afterwards anyway. On a second note, invoking close() on the input stream shouldn't be closing the socket. Are you sure that the socket is closed when you call close() on the input stream? May be the connection is (incorrectly) closed by the peer when it notices that its output stream (your input stream) has been closed.

A solution that avoids reading until the end of the input stream is to delimit the input message somehow. For example, you could send the length of the message before the message, then read the whole message from the stream into a buffer, then parse the buffer using XMLReader, or create a special InputStream that takes that reaches its end after the specified amount of data. Or you could add a special delimiter in the stream after the message, then you'd create a special InputStream reaches its end when the underlying stream produces the delimiter.

云裳 2024-07-15 05:13:11

由于每个xml文档只有一个根节点。 因此,扫描该节点的末尾并在之后关闭流。

Since each xml document has only one root node. So, scan the end of this node and close the stream after.

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