Java TCP连接提示java.net.SocketException: 你的主机中的软件中止了一个已建立的连接。
如图

检查了一下代码,Socket客户端不会报错,就是ServerSocket端报错。
...
回合263:已发送1346560字节,完成3.893933%
Exception in thread "main" java.net.SocketException: 你的主机中的软件中止了一个已建立的连接。
at java.base/sun.nio.ch.NioSocketImpl.implWrite(NioSocketImpl.java:413)
at java.base/sun.nio.ch.NioSocketImpl.write(NioSocketImpl.java:433)
at java.base/sun.nio.ch.NioSocketImpl$2.write(NioSocketImpl.java:812)
at java.base/java.net.Socket$SocketOutputStream.write(Socket.java:1120)
at java.base/java.io.OutputStream.write(OutputStream.java:124)
at client.send(client.java:44)
at client.main(client.java:18)
进程已结束,退出代码为 1
看了一下代码,也没有什么问题

public static void send() throws IOException {
File sendFile;
System.out.print("请输入要传输的文件路径:");
String local = get.next();
sendFile = new File(local);
// int port = new Random().nextInt(1000,2000);
int port = 7825;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("已准备好,端口" + port + ",等待客户端连接");
Socket socket = serverSocket.accept();
System.out.println("已连接,准备传输");
OutputStream out = socket.getOutputStream();
int roundByte = 5120;
byte[] m = new byte[roundByte];
long send = 0;
long round = 0;
long size = sendFile.length();
FileInputStream in = new FileInputStream(sendFile);
do {
int l = in.read(m);
out.write(m);
send += roundByte;
round++;
float now = (float) send / size * 100;
System.out.println("回合" + round + ":已发送" + send + "字节,完成" + now + "%");
//System.out.println("Debug:发送数据" + Arrays.toString(m));
} while (send <= size);
out.write("$END".getBytes());
System.out.println("发送完成");
socket.close();
serverSocket.close();
}
请问有哪位知道,真的改了很久了。

