0

I am dealing with a Java method that stops working silently. I've put catches around the method and am trying to catch Throwables, but I don't catch anything. I also tried debugging, but the method just stops working on a line for no reason.

I have a simple client-server app and the client sends a file to the server. Here's the relevant code for the server handling the file transfer:

public void receiveFile(String filePath) throws IOException
    {
        InputStream inStream = null;
        FileOutputStream outStream = null;

        try {
            inStream = fSocket.getInputStream();

            File f = new File(filePath);
            if (f.exists()) {
                throw new FileAlreadyExistsException(filePath + " already exists!");
            }

            outStream = new FileOutputStream(filePath);

            byte[] bytes = new byte[8192];

            int count;
            while ((count = inStream.read(bytes)) > 0) {
                outStream.write(bytes, 0, count);
            }
        } catch (Throwable t) {
            System.out.println("Error when fetching file");
            System.out.println(t.toString());
        } finally {
            if (outStream != null) outStream.close();
        }
    }

I can get the file fine. When I look in my files, the file has been transfered and is complete. For some reason, as soon as the transfer is done, my problem comes with the line while ((count = inStream.read(bytes)) > 0). I tried debugging, and when it gets to that line when the transfer is done, it just stops. I have no idea where the execution goes. Here's a gif of the issue.

Does anyone know what this could be? I am using threads if that's of any relevance.

Here's the client code:

public void sendFile(String fileName) throws IOException
    {
        InputStream inStream = null;
        OutputStream outStream = null;

        try {
            File file = new File(fileName);
            byte[] buffer = new byte[8192];
            inStream = new FileInputStream(file);
            outStream = fSocket.getOutputStream();

            int count;
            while ((count = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, count);
            }
            System.out.println("Done sending data");
        } catch (IOException e) {
            System.out.println(e.toString());
        } finally {
            if (outStream != null) outStream.flush();
            if (inStream != null) inStream.close();
        }
    }
ThomasFromUganda
  • 380
  • 3
  • 17
  • 2
    Does the execution really stop? I didn't see clearly in your gif, but it seems to me that it was waiting for an operation to finish (namely `inStream.read`). Try "Debug -> Break all" (or something like this). –  Oct 14 '19 at 01:02
  • 1
    @dyukha You can see on the symbols on the left side of the console that the program is still running and waiting for input. So you're correct here. – Tom Oct 14 '19 at 01:06
  • hi @ThomasFromUganda, `fSocket.getInputStream();` where does `fSocket`originates from? it might be the server where you requesting the stream from is stuck or never closes the connection. may we know what are you trying to do? – Bagus Tesa Oct 14 '19 at 01:12
  • @BagusTesa Hi there, I am not closing the socket, but I am flushing the output stream when I am done sending the file as I am showing in the edit I just made. I want to keep the socket connection alive and send a file. – ThomasFromUganda Oct 14 '19 at 01:14
  • 1
    thats the problem, the server flushes the stream, while the **client still waiting for the next bytes** (waiting for input as @Tom explained). you might want to send special bytes to tell the client that you are finished. – Bagus Tesa Oct 14 '19 at 01:19

0 Answers0