java - 线程中的异常 "main"java.net.SocketException : Connection reset -

标签 java networking file-io stream client-server

我创建了一个简单的客户端/服务器程序,其中客户端从命令行参数获取文件。然后,客户端将文件发送到服务器,在服务器中使用 GZIP 对其进行压缩并发回客户端。

服务器程序第一次运行时很好,并且没有产生任何错误,但在运行客户端后我收到错误。

我收到一条错误消息,表示连接已重置,并且我尝试了许多不同的端口,所以我想知道我的代码或关闭流的时间是否有问题?

任何帮助将不胜感激!

编辑 - 对两个程序进行了更改。

客户:

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

//JZip Client

public class NetZip {

    //Declaring private variables.
    private Socket socket = null;
    private static String fileName = null;
    private File file = null;
    private File newFile = null;
    private DataInputStream fileIn = null;
    private DataInputStream dataIn = null;
    private DataOutputStream dataOut = null;
    private DataOutputStream fileOut = null;


    public static void main(String[] args) throws IOException {
    try {
        fileName = args[0];
        }
    catch (ArrayIndexOutOfBoundsException error) {
        System.out.println("Please Enter a Filename!");
    }
    NetZip x = new NetZip();
    x.toServer();
    x.fromServer();

    }

    public void toServer() throws IOException{
    while (true){
    //Creating socket
       socket = new Socket("localhost", 4567);
       file = new File(fileName);

       //Creating stream to read from file.
       fileIn = new DataInputStream(
           new BufferedInputStream(
               new FileInputStream(
                   file)));

       //Creating stream to write to socket.
       dataOut = new DataOutputStream(
           new BufferedOutputStream(
               socket.getOutputStream()));

       byte[] buffer = new byte[1024];

           int len;
           //While there is data to be read, write to socket.
        while((len = fileIn.read(buffer)) != -1){
            try{
            System.out.println("Attempting to Write " + file
                + "to server.");
            dataOut.write(buffer, 0, len);
            }
            catch(IOException e){
            System.out.println("Cannot Write File!");
            }
           } 
        fileIn.close();
        dataOut.flush();
        dataOut.close();

        }

    }
  //Read data from the serversocket, and write to new .gz file.
    public void fromServer() throws IOException{

    dataIn = new DataInputStream(
           new BufferedInputStream(
                   socket.getInputStream()));

    fileOut = new DataOutputStream(
           new BufferedOutputStream(
               new FileOutputStream(
                   newFile)));

    byte[] buffer = new byte[1024];

        int len;
        while((len = dataIn.read(buffer)) != -1){
            try {
            System.out.println("Attempting to retrieve file..");
            fileOut.write(buffer, 0, len);
            newFile = new File(file +".gz");

            }
            catch (IOException e ){
            System.out.println("Cannot Recieve File");
            }
        } 
        dataIn.close();
        fileOut.flush();
        fileOut.close();
        socket.close();

    }


}

服务器:

import java.io.*;
import java.net.*;
import java.util.zip.GZIPOutputStream;

//JZip Server

public class ZipServer {

    private ServerSocket serverSock = null;
    private Socket socket = null;
    private DataOutputStream zipOut = null;
    private DataInputStream dataIn = null;

    public void zipOut() throws IOException {

    //Creating server socket, and accepting from other sockets.
    try{
    serverSock = new ServerSocket(4567);
    socket = serverSock.accept();
    }
    catch(IOException error){
        System.out.println("Error! Cannot create socket on port");
    }

    //Reading Data from socket
    dataIn = new DataInputStream(
           new BufferedInputStream(
                   socket.getInputStream()));

    //Creating output stream.
    zipOut= new DataOutputStream(
        new BufferedOutputStream(
            new GZIPOutputStream(
                socket.getOutputStream())));

    byte[] buffer = new byte[1024];

        int len;
        //While there is data to be read, write to socket.
        while((len = dataIn.read(buffer)) != -1){
            System.out.println("Attempting to Compress " + dataIn
                + "and send to client");
            zipOut.write(buffer, 0, len);
        } 
        dataIn.close();
        zipOut.flush();
        zipOut.close(); 
        serverSock.close();
        socket.close();
    }

    public static void main(String[] args) throws IOException{
    ZipServer run = new ZipServer();
    run.zipOut();

    }

}

错误消息:

Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at java.io.DataInputStream.read(DataInputStream.java:100)
    at ZipServer.<init>(ZipServer.java:38)
    at ZipServer.main(ZipServer.java:49)

最佳答案

首先,发生错误是因为客户端在发送任何数据之前失败并结束,因此 连接在服务器想要读取时关闭。
发生错误的原因是您将 File 对象分配给未使用的局部变量(您的编译器没有警告吗?)

public File file = null;
public File newFile = null;

public static void main(String[] args) throws IOException {
try {
    String fileName = args[0];
    File file = new File(fileName);
    File newFile = new File(file +".gz");
    } 
catch (ArrayIndexOutOfBoundsException error) {
    System.out.println("Please Enter a Filename!");
}

但是在您的 toServer 方法中,您使用类变量 file 作为 FileInputStream 的参数,并且该变量为 null,这会导致错误并结束程序。

其次,如果完成了对输出流的写入,则应该调用

socket.shtdownOutput();

否则,服务器会尝试读取,直到发生超时。

关于java - 线程中的异常 "main"java.net.SocketException : Connection reset -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282997/

相关文章:

javascript - JavaScript中多向网络的路由或寻路算法

.net - System.IO.FileSystemWatcher 的底层是如何工作的?

java - 封装、访问器 C# 与 Java

java - 设置光标在 jframe 中的位置

docker - 在 docker 容器外访问 vespa 的问题

c++ - 如果我想从输入文件(例如1、2、3)中读取逗号分隔的数据,包括逗号?

python - 在Python中查找扩展名为.txt的目录中的所有文件

java - 我可以使用 Wicket 动态添加永久行吗?

java - 从警报启动特定 Activity - 即使应用程序关闭

linux - 按字节限制的 tc-sfq 替代方案?