1

I am trying to transfer a file from server to client using Java and TCP, however on the client-side I am getting a socket closed exception, whereas the server has no errors when attempting to transfer the file. I am confused about this error because I did not close the socket before trying to read from it. The server accepts the connection and sends the file, but the client gets that error. Any suggestion?

The error is:

java.net.SocketException: Socket closed

Server thread's run function:

public void run() {
    System.out.println("Service thread running for client at " + socket.getInetAddress() + " on port " + socket.getPort());
    try {
        File file = new File("hank.txt");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = socket.getOutputStream();

        byte[] contents;
        long fileLength = file.length();
        long current = 0;

        long start = System.nanoTime();
        while(current!=fileLength) {
            int size = 1000;
            if(fileLength - current >= size) {
                current += size;
            }
            else {
                size = (int)(fileLength - current);
                current = fileLength;
            }
            contents = new byte[size];
            bis.read(contents,0,size);
            os.write(contents);
            System.out.println("sending file..." + (current*100)/fileLength+"% complete!");
        }
        os.flush();
        this.socket.close();
    }catch(Exception e) {
        e.printStackTrace();
    }
}

Client receiving the file code:

    System.out.println("Going to get the file...");
    socket = new Socket(response.getIP().substring(1), response.getPort());

    byte[] contents = new byte[10000];
    FileOutputStream fos = new FileOutputStream("hank.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    InputStream in = socket.getInputStream();

    int bytesRead = 0;
    System.out.println("Starting to read file...");
    while((bytesRead = is.read(contents))!=-1) { //the error points to this lin
        bos.write(contents,0,bytesRead);
    }

    bos.flush();
    bos.close();
    in.close();

    //
    socket.close();
assembler
  • 3,098
  • 12
  • 43
  • 84
hjskeqwe
  • 175
  • 1
  • 9
  • Change is.read(contents)) to in.read(contents)) – Vishnu T S Dec 04 '17 at 20:11
  • Thank you so much that fixed it. I have an ObjectInputStream called "is" so it didn't get underline in red. I never would have found that – hjskeqwe Dec 04 '17 at 20:14
  • See [Java multiple file transfer over socket](https://stackoverflow.com/questions/10367698/java-multiple-file-transfer-over-socket) for better code. – user207421 Dec 04 '17 at 21:09

1 Answers1

1

Input stream for this socket is available in variable in

InputStream in = socket.getInputStream();

So

 Change is.read(contents)) to in.read(contents)) 
Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
  • 1
    That worked, thank you so much. I feel like an idiot I've been staring at this for like 30 minutes. – hjskeqwe Dec 04 '17 at 20:15