Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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){
            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.
    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)
                You don't need the while(true) loop in the toServer() method.  You don't need the DataOutputStream or the DataInputStream. flush() before close() is redundant.
– user207421
                Dec 4, 2014 at 0:23
                @EJP Thanks, i've got rid of all the flushes and the data streams. However it still doesn't seem to be working? Is it worth mentioning that i'm running Ubuntu and using Eclipse. I've got no compiler errors or warning flags and the error is the same.  At the bottom of the error it says;  at ZipServer.zipOut(ZipServer.java:38) at ZipServer.main(ZipServer.java:53)  which point to; 	while((len = dataIn.read(buffer)) != -1){  and 	run.zipOut(); (in the main method)  Could it be anything to do with these?
– SmithsonG
                Dec 4, 2014 at 0:36
                Those remarks were comments, not answers. And don't repost questions just because you didn't get an answer the first time.
– user207421
                Dec 4, 2014 at 0:38
                Sorry about that I didn't realise I had reposted. I accidentally pressed enter rather than shift+enter when trying to respond in a comment. May have been because I had the page open in multiple tabs. @EJP
– SmithsonG
                Dec 4, 2014 at 0:43

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.

He doesn't need to call shutdownOutput() if he is closing the socket, and no timeout will occur as he isn't setting one. – user207421 Dec 4, 2014 at 0:24 @EJP Does closing socket solve the problem? I am not sure if I should close the connection because the server has to keep on listening to clients. I'm experiencing the same problem and my server is simple, it accepts clients applications and returns calls a method that returns a strong message to the client, so communication works fine. One strange thing though, if I shut down the client, the server dies too and pulls out Connection Reset error. My server has a while loop that keeps listening for client connections and if any connection happen it returns the string – crakama Jan 18, 2018 at 11:09

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.