简体   繁体   中英

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.

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?)

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.

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 .

So what you can do is just copy the apache maven folder and paste it in the wrapper folder inside the project.

It will manually download the apache maven, and it will definitely work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM