从 java ServerSocket 到 iphone 的网络问题(仅在 3g 模式下)
我遇到了奇怪的行为:
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您发布服务器的 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
我通过用 BufferedOutputStream 包装套接字输出流解决了这个问题:
谢谢,
利奥尔
I solved that issue, by wrapping the socket output stream with a BufferedOutputStream:
Thanks,
Lior