如何修复线程"main"中的"Exception"java.net.SocketException:“连接重置" [英] How to fix "Exception in thread "main" java.net.SocketException: Connection reset"

查看:85
本文介绍了如何修复线程"main"中的"Exception"java.net.SocketException:“连接重置"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个可以发送和接收文件的Java程序.

I am trying to set up a java program that would send and receive a file.

我知道此网站上有与此问题类似的事情,但是阅读这些问题后我遇到了麻烦.

I know there are similar things for this problem on this website, but I was having trouble after reading those.

我关注了此视频: https://www.youtube.com/watch?v = WeaB8pAGlDw& ab_channel = Thecodersbay

它在他的视频中有效,我不确定自己做错了什么.第一个Java文件运行正常,但是当我尝试运行第二个Java文件时,出现此错误:

It worked in his video, and I am not sure what I did wrong. The first java file runs just fine, but when I try to run the second I get this error:

Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at fileSender.fileclient.main(fileclient.java:19)

我尝试使用其他端口,但是并不能解决问题.

I tried using some other ports, but that did not solve the problem.

这是我现在拥有的:

文件服务器文件:

package fileSender;

import java.io.*; 
import java.net.*; 

public class fileserver 
{
private static ServerSocket s;
private static FileInputStream fr;

public static void main(String[] args) throws Exception
{
    s = new ServerSocket(1418);
    Socket sr = s.accept();   //accept the connection

    fr = new FileInputStream("C:\\Users\\Anon\\Desktop\\folder\\testfile.txt");
    byte b[] = new byte[2002];   //before reading, a byte has to be declared. byte data includes the size of the file. if the size is unknown, use a random one I guess 
    fr.read(b, 0, b.length);   //the byte b, start reading the file at 0, and stop reading at the end of it. read from start to finish, and store it as b 

    OutputStream os = sr.getOutputStream();   
    os.write(b, 0, b.length);   //b variable will be sent with this. again, starts at 0, and ends at the length of the byte b 


}

}

这是客户端文件:

package fileSender;

import java.io.*;   //the whole thing
import java.net.*; 

public class fileclient 
{
private static Socket sr;
private static FileOutputStream fr;

public static void main(String[] args) throws Exception
{
    byte []b = new byte[2002];   //size from earlier. what the person gets 

    sr = new Socket("localhost",1418);
    InputStream is = sr.getInputStream();   //capturing the stream

    fr = new FileOutputStream("C:\\Users\\Anon\\Desktop\\testfile.txt");
    is.read(b, 0, b.length);   //will capture the stream of "is". again, whole file, 0 to end 

    fr.write(b, 0, b.length);   //writes the whole content into a file 

}
}

我做了很多评论,以便使事情变得有意义.

I tried to comment a lot so that I can make sense of things.

先谢谢您了:)

推荐答案

因此,连接重置表示套接字已从另一端关闭.考虑到您的服务器"在做什么,这是很合逻辑的.

So, Connection Reset means that the socket was closed from the other end. This is quite logical considering what your "server" is doing.

当客户端连接时,您的服务器正在接受连接,从文件读取最多2002字节,将其发送到客户端并终止应用程序.届时,套接字 sr 将会与应用程序的其余资源一起关闭.此时,仍在从 InputStream 读取的客户端将收到通知,通知该套接字不再有效,并且抛出该异常.

When the client connects, your server is accepting the connection, reading up to 2002 bytes from a file, sending it to the client and terminating the application. At that point the socket sr will just be closed together with the rest of the application's resources. At this point, the client which is still reading from the InputStream would get notified that the socket is no longer valid, and that exception is thrown.

您应该检查是否成功编写了 testfile.txt .也许可以,尽管我不会让服务器突然断开连接.我会让客户端正常关闭,或者在不活动后使客户端连接超时,因为在从TCP缓冲区读取所有数据之前,您可能会遇到出现 Connection Reset 错误的风险.(TCP错误的传达速度通常更快.)

You should check whether testfile.txt is written successfully or not. It might be OK, although I wouldn't have the server disconnect so abruptly. I would let the client close gracefully, or timeout the client connection after inactivity, because you might risk getting that Connection Reset error before reading all the data from the TCP buffers. (TCP Errors tend to be communicated faster.)

这篇关于如何修复线程"main"中的"Exception"java.net.SocketException:“连接重置"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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