Java套接字异常:recv失败
这基本上是图片: 我有一个服务器和一个客户端(通过本地主机操作)。服务器为每个连接创建一个客户端线程。这个新线程在读取时阻塞,等待来自客户端的消息。我可以随意通过 GUI 停用服务器,它(从主线程)向客户端发送“断开连接”,然后关闭输出流,以唤醒被阻止的客户端线程,而客户端线程又通过关闭套接字来完成(我相信在关闭输出流后这种关闭是不必要的,但无论如何)。
客户端: 连接请求后,它会休眠 10 秒,写入断开消息并逐个字符读取应答。
问题:
在 10 秒客户端睡眠时间内停用服务器后,客户端从服务器正确读取“断开连接”消息。但是,如果我只是为每个读取的字符添加一个虚拟打印(在 while 循环内),则最终读取的结果会有所不同。 有时它会正确读取服务器的“断开连接”,有时它会读取“disco”或类似的变体,并抛出此异常:
“Java异常:“软件导致连接中止:recv失败”
关于为什么添加一些的任何建议prints 创建这个结果?我假设另一端有一个关闭的套接字不会对读取消息产生影响。 我发现的有关接收错误的其他线程提到了超时,我猜想在使用本地主机时不应该真正发生这种情况?
This is basically the picture:
I have a server and a client (operating through localhost). Server creates a clientthread for each connection. this new thread blocks on read waiting for messages from client. At will, i can deactivate the server through the GUI, which sends (from a main thread) "disconnect" to the client, and then closes the output stream, to wake up the blocked clientthread, which in turn finishes up by closing the socket (I believe this closing is unnecessary after closing outputstream but anyway).
Client side:
After connection request, it sleeps 10 seconds, writes a disconnect message and reads the answer char by char.
The problem:
After deactivating the server within that 10 second client sleep time, client reads the "disconnect" message correctly from the server. However, If I just add a dummy print for each char read (inside the while loop), the result of the final read is varying.
Sometimes it will read the server's "disconnect" correctly, sometimes it will read "disco", or a similar variation, and throw this exception:
"Java exception : "Software caused connection abort: recv failed"
Any suggestions as to why adding a few prints creates this result? I would assume having a closed socket on the other end wouldn't have an effect on reading the message.
The other threads I found about recv errors mentioned timeouts, which I'm guessing shouldn't really be happening when using localhost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生该错误是因为另一端已断开连接并且您的写入失败。如果您阅读得足够快,它还没有确定连接已丢失。
有多种方法可以避免此错误,但最简单的方法就是忽略它
The error occurs because the other end has disconnected and your write has failed. If you read quickly enough, it hasn't determined the connection has been lost.
There are ways to avoid this error, but the simplest thing to do is to ignore it
最近看到这个错误是由于 JBoss 配置(即standalone.xml)中缺少代理设置引起的。如有必要,请向您的系统属性添加代理 - 您可以通过使用 SoapUI 调用 Web 服务来检查是否需要它们。
Recently saw this error caused by a lack of proxy set in JBoss config (I.e. standalone.xml). Add a proxy to your system properties if necessary - you can check if you need them by calling the web service with SoapUI.