java.net.SocketException: 套接字已关闭 TCP 客户端服务器通信 [英] java.net.SocketException: socket closed TCP Client Server Communication

查看:48
本文介绍了java.net.SocketException: 套接字已关闭 TCP 客户端服务器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 TCP 创建一个 Java 客户端/服务器应用程序,其中我们有两个套接字:

I am creating a java client/server application over TCP where we have two sockets:

  • 一种用于交换消息.
  • 两个用于文件传输.

我在服务器中创建了两个 ServerSockets 以便创建套接字一和二
通过接受 ServerSockets.

I have created in the Server two ServerSockets in order to create Socket One and Two
by accepting the ServerSockets.

首先客户端通过第一个 Socket 发送一些字节
以便它可以告诉服务器它需要哪个文件.

At first the client sends some bytes through the first Socket
so that it can tell the Server which file it needs.

然后Server通过第二个socket将文件发送给客户端.
客户端收到文件后,尝试将传输完成消息发送回服务器.

在那里我得到了关闭套接字的异常.
但是直到现在我才关闭套接字.
我只关闭发送文件的缓冲区.
即使我在发送文件后再次尝试打开socketInputStream,错误仍然相同.
此外,如果我不关闭发送文件的缓冲区,客户端将无法获取文件.

Then the Server through the second socket sends the file to the client.
After the client receives the file tries to send back to the server a Transfer Done Message.

There I get the Exception for the closed socket.
However I never close the socket up until now.
I only close the buffer which sents the file.
Even if I try to open again the socketInputStream after I sent the file the error is still the same.
Also if I do not close the buffer which sents the file, the client does not get the file.

服务器异常

返回消息错误 - java.net.SocketException: 套接字已关闭

Error in Return Message - java.net.SocketException: socket closed

客户端异常

服务器响应 - java.net.SocketException:软件导致连接中止:套接字写入错误

Server response - java.net.SocketException: Software caused connection abort: socket write error

我做错了什么?

推荐答案

套接字关闭"异常意味着捕获异常的应用程序关闭了套接字,然后继续尝试使用它.您可能不知道关闭套接字的输入或输出流也会关闭另一个流和套接字.对于软件导致的连接中止",请参阅重复链接.

A 'Socket closed' exception means that the application that caught the exception closed the socket and then kept trying to use it. You may be unaware that closing the input or output stream of a socket closes the other stream and the socket as well. For 'software caused connection abort' see the duplicate link.

这篇关于java.net.SocketException: 套接字已关闭 TCP 客户端服务器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆