简体   繁体   English

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

[英]Exception in thread “main” java.net.SocketException: Connection reset -

I have created a simple Client/Server program where the client takes a file from command line arguments. 我创建了一个简单的客户端/服务器程序,其中客户端从命令行参数获取文件。 The client then sends the file to the server, where it is compressed with GZIP and sent back to the client. 然后,客户端将文件发送到服务器,在服务器上使用GZIP对其进行压缩并发送回客户端。

The server program when ran first is fine, and produces no errors but after running the client I get the error. 第一次运行时,服务器程序很好,不会产生任何错误,但是在运行客户端之后,我得到了错误。

I am getting an error saying the connection is reset, and i've tried numerous different ports so i'm wondering if there is something wrong with my code or time at which i've closed the streams? 我收到一条错误消息,说连接已重置,我尝试了许多不同的端口,所以我想知道我关闭流的代码或时间是否有问题?

Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT - Made changes to both programs. 编辑-对两个程序进行了更改。

Client: 客户:

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();

    }


}

Server: 服务器:

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();

    }

}

Error Message: 错误信息:

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)

First, the error occurs because the client fails and ends before sending any data, so that the connection is closed at the time the server wants to read. 首先,发生错误是因为客户端发生故障并在发送任何数据之前结束,因此在服务器要读取时关闭了连接。
The error occurs because you assign the File objects to unused local variables (did your compiler not warn?) 发生错误是因为您将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!");
}

but In your toServer method you use the class variable file as parameter for FileInputStream and this variable is null and this results in an error which ends the program. 但是在您的toServer方法中,您将类变量文件用作FileInputStream的参数,并且该变量为null,这将导致错误,从而结束程序。

Second, if you finished the writing to the outputstream, you should call 其次,如果完成了对输出流的写入,则应调用

socket.shtdownOutput();

Otherwise, the server tries to read until a timeout occurs. 否则,服务器将尝试读取直到发生超时。

Problem is that server is not able to download apache maven . 问题是服务器无法下载apache maven

So what you can do is just copy the apache maven folder and paste it in the wrapper folder inside the project. 因此,您只需复制apache maven文件夹并将其粘贴到项目内的wrapper文件夹中即可。

It will manually download the apache maven, and it will definitely work. 它将手动下载apache maven,它肯定可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:线程“main”中的异常java.net.SocketException:连接重置 - Flutter : Exception in thread "main" java.net.SocketException: Connection reset 线程“主”java.net.SocketException 中的异常:java.net.SocketInputStream.read 处的连接重置(未知来源) - Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) 线程“主”中的异常java.net.SocketException:在java.io.BufferedReader.readLine重置连接(未知源) - Exception in thread “main” java.net.SocketException: Connection reset at java.io.BufferedReader.readLine(Unknown Source) 如何修复“线程” main”中的异常“ java.net.SocketException:连接重置” - How to fix “Exception in thread ”main“ java.net.SocketException: Connection reset” 线程“主”中的异常org.openqa.selenium.WebDriverException:java.net.SocketException:连接重置 - Exception in thread “main” org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset 异常java.net.SocketException:连接重置 - Exception java.net.SocketException: Connection reset 错误java.net.SocketException:连接重置 - ERROR java.net.SocketException: Connection reset java.net.SocketException:连接重置“”“ - java.net.SocketException: Connection reset “”" java.net.SocketException:连接重置 - java.net.SocketException: Connection reset java.net.SocketException:连接重置jsoup - java.net.SocketException: Connection reset jsoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM