从 java ServerSocket 到 iphone 的网络问题(仅在 3g 模式下)

发布于 2024-11-07 18:23:00 字数 686 浏览 0 评论 0原文

我遇到了奇怪的行为:

我有一个基于 java ServerSocket 的服务器,它为从 iPhone 设备发送的 TCP 请求提供服务。 我发现当我使用iPhone通过Wi-Fi连接时,网络没有问题。 iPhone 打开一个连接,然后按其应有的方式从流中写入和读取,没有明显的延迟。到目前为止,一切都很好。 然而,使用 3g 时,从发送第一条消息到响应完全返回,我遇到了 1.5 秒的延迟。看起来消息的第一个字节是在服务器将消息刷新到套接字后立即读取的,但消息的其余部分被延迟了大约一秒钟(!)。

我通过打印 iPhone 端的缓冲区大小以及在服务器端使用wireshark 监控网络来验证此网络行为。 Wireshark 也显示了这一点 - 第一个字节以 1 字节消息大小发送(它始终为 0 BTW),消息的其余部分随后以一个或多个大消息发送( ~1300 字节)。

该消息由 8 字节标头(2 个整数 - 类型和大小)和该大小的正文组成。

真正奇怪的是,在 C++ 中编写使用相同协议的类似服务器(使用 poco 服务器)工作得很好。

我使用套接字的 DataOutputStream 使用 2 个 writeInt 指令和一个write(byte[]) 写入主体。然后我冲洗溪流。

有谁知道是否有一个套接字选项(在java中)可以导致这种行为?

任何帮助将不胜感激。

I am experiencing weird behavior:

I have a server, based on a java ServerSocket that serves TCP request that are send from iPhone device.
I have noticed that when I use the iPhone is connected via wi-fi, there are no problems with the networking. The iphone opens a connection and than write and read from the stream as it should, with no noticeable latency. So far so good.
Working with 3g, however, I am experiencing 1.5 seconds latency from the second the first message is sent until the response is fully arriving back. It seems that the first byte of the message is read right after the server flushes the message to the socket, but the rest of the message is being delayed for about a second(!).

I verified this network behaviour by both printing the buffer size that arraive on the iphone side and by monitoring the networking with wireshark on the server side.
Wireshark shows this as well - the first byte, is sent in a 1 byte message size (it is always 0 BTW), and the rest of the message is sent afterwards, in one or more large messages (~1300 bytes).

The message is composed of an 8 byte header (2 integers - type and size), and a body of that size.

The real weird thing is that writing similar server that uses the same protocol in c++ (using poco server), is working perfectly.

I use the DataOutputStream of the socket to write the header using 2 writeInt instructions and a write(byte[]) that writes the body. I then flush the stream.

Does anyone knows if there's a socket option (in java) that can cause this kind of behaivour?

Any help will be appreciated.

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

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

发布评论

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

评论(2

转身泪倾城 2024-11-14 18:23:01

我建议您发布服务器的 Wireshark 跟踪,因为这可以让您深入了解配置了哪些 TCP 套接字选项以及从握手到第一次数据传输的时间线

I suggest that you post your server's Wireshark trace as this may provide some insight into what TCP socket options are configured and the timeline from the handshake to the first data transer

寄离 2024-11-14 18:23:00

我通过用 BufferedOutputStream 包装套接字输出流解决了这个问题:

DataOutputStream dos = new DataOutputStream(new BufferedOutputstream(sock.getOutputStrean()));

谢谢,
利奥尔

I solved that issue, by wrapping the socket output stream with a BufferedOutputStream:

DataOutputStream dos = new DataOutputStream(new BufferedOutputstream(sock.getOutputStrean()));

Thanks,
Lior

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