Java Socket 无法通过网络传输数据
我在 Java Knopflerfish 客户端包和 PHP(CLI,而不是 Web)服务器之间的套接字上遇到了一个奇怪的问题。
当客户端/服务器对都位于本地主机时,客户端/服务器对工作正常,并且所有数据都成功传输。但是,当 Java 客户端位于不同的计算机上时,与服务器的连接成功,但 PHP 脚本没有收到任何数据。数据包分析证实 Java 客户端发送的数据已被服务器接收 - PHP 似乎在获取它时遇到了问题。
进一步说明,我使用 telnet 作为客户端进行了一些测试。 PHP 服务器脚本可以从任何主机接收所有数据。这让我相信问题与 java 设置套接字的方式有关,或者存在一些我不熟悉的网络问题。任何想法将不胜感激。如果需要,可以发布代码示例。
I'm experiencing a bizarre problem with sockets between a Java Knopflerfish client bundle and a PHP (CLI, not web) server.
The client/server pair work fine when both are located on the localhost, and all data is transmitted successfully. However, when the Java client exists on a different machine, connections to the server are successful, but no data is received by the PHP script. Packet analysis confirms that the data sent by the Java client is received on by the server - PHP just seems to have problems getting its hands on it.
As a further note, I've done some tests with telnet as the client. The PHP server script receives all data fine from any host. This leads me to believe that the problem has something to do with the way java is setting up the socket or that there is some networking issue that I'm not familiar with. Any thoughts would be appreciated. Can post code samples if desired.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能忘记刷新客户端的缓冲区。 telnet 对每一行都会进行刷新,这样就可以解释它的工作原理。
You probably forgot to flush the buffers on the client side. telnet does a flush for every line, so that would explain why it works.
事实证明这是一个竞争条件。如果我在收到连接后和执行 socket_read 之前休眠几毫秒,它就能很好地完成任务。
Turns out it was a race condition. If I sleep for a few milliseconds after receiving a connection and before doing a socket_read, it does the trick nicely.