“线程中的异常”;“主要”;java.net.SocketException:连接重置-

“线程中的异常”;“主要”;java.net.SocketException:连接重置-,java,networking,file-io,stream,client-server,Java,Networking,File Io,Stream,Client Server,我创建了一个简单的客户机/服务器程序,其中客户机从命令行参数获取一个文件。然后,客户机将文件发送到服务器,在服务器上用GZIP压缩并发送回客户机 服务器程序在第一次运行时是正常的,并且不会产生错误,但是在运行客户端之后,我得到了错误 我收到一个错误,说连接重置了,我尝试了许多不同的端口,所以我想知道我的代码或关闭流的时间是否有问题 任何帮助都将不胜感激 编辑-对两个程序所做的更改 客户: import java.io.*; import java.net.*; //JZip Client p

我创建了一个简单的客户机/服务器程序,其中客户机从命令行参数获取一个文件。然后,客户机将文件发送到服务器,在服务器上用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)
线程“main”java.net.SocketException中的异常:连接重置 位于java.net.SocketInputStream.read(SocketInputStream.java:196) 位于java.net.SocketInputStream.read(SocketInputStream.java:122) 在java.io.BufferedInputStream.fill处(BufferedInputStream.java:235) 位于java.io.BufferedInputStream.read1(BufferedInputStream.java:275) 在java.io.BufferedInputStream.read处(BufferedInputStream.java:334) 读取(DataInputStream.java:100) 在ZipServer上。(ZipServer.java:38) 位于ZipServer.main(ZipServer.java:49)
首先,发生错误的原因是客户端失败并在发送任何数据之前结束,因此 服务器要读取时,连接已关闭
发生此错误的原因是将文件对象分配给未使用的局部变量(编译器是否未发出警告?)

但是在toServer方法中,您使用类变量file作为FileInputStream的参数,该变量为null,这将导致一个错误,从而结束程序

其次,如果您完成了对outputstream的写入,那么应该调用

socket.shtdownOutput();

否则,服务器将尝试读取,直到出现超时。

问题是服务器无法下载apache maven

因此,您可以做的只是复制ApacheMaven文件夹并将其粘贴到项目内的包装器文件夹中


它将手动下载apache maven,它肯定会工作。

好的,在您编辑之后,我的答案的第一部分已经过时:)如果他正在关闭套接字,他不需要调用
shutdownOutput()
,并且不会发生超时,因为他没有设置超时。@EJP关闭套接字是否解决了问题?我不确定是否应该关闭连接,因为服务器必须继续侦听客户端。我也遇到了同样的问题,我的服务器很简单,它接受客户端应用程序并返回调用一个方法,该方法向客户端返回一条强消息,因此通信工作正常。但有一件奇怪的事,如果我关闭客户端,服务器也会死机,并拉出连接重置错误。我的服务器有一个while循环,它一直在侦听客户端连接,如果发生任何连接,它将返回stringIt并没有真正解决问题。我仍然有相同的错误。在
toServer()
方法中,您不需要
while(true)
循环。您不需要
DataOutputStream
DataInputStream。
close()之前的
flush()
是多余的。@EJP谢谢,我已经消除了所有的flush和数据流。然而,它似乎仍然不起作用?值得一提的是,我正在运行Ubuntu并使用Eclipse。我没有编译器错误或警告标志,错误是相同的。在错误的底部,它说;在ZipServer.zipOut(ZipServer.java:38)在ZipServer.main(ZipServer.java:53)中,指向;while((len=dataIn.read(buffer))!=-1){and run.zipOut();(在main方法中)这可能与这些有关吗?这些评论是评论,而不是答案。不要仅仅因为第一次没有得到答案就转发问题。很抱歉,我没有意识到我已经转发了。在尝试回复评论时,我不小心按了enter而不是shift+enter。可能是因为我在mu中打开了页面多个选项卡。@EJP
socket.shtdownOutput();