0

I am trying to transfer data via Socket from android phone to server.I read that Closing either the input or output stream of a socket closes another stream and the socket. And I commented out the given lines. But the error appears in the logs.

UPD But if I upload a file up to 3 KB, then the upload is successful

ERROR App:

java.net.SocketException: Socket is closed

ERROR Server:

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
            @Override
    public void run() {
        try {
            byte[] request = new byte[4096];
            byte[] reply = new byte[8192];
            final InputStream inFromClient = sClient.getInputStream();
            final OutputStream outToClient = sClient.getOutputStream();
            SSLSocket remoteSocket = tlsConnectionService.createSSLSocket(remoteHost, remotePort);
            final InputStream inFromServer = remoteSocket.getInputStream();
            final OutputStream outToServer = remoteSocket.getOutputStream();
            // a new thread for uploading to the server
            new Thread() {
                public void run() {
                    int bytes_read;
                    try {
                        while ((bytes_read = inFromClient.read(request)) != -1) {
                            String newReq = new String(request);
                            outToServer.write(newReq.replace(LOCAL_SOCKET_URL, remoteHost).getBytes(), 0, bytes_read);
                            outToServer.flush();
                        }
                    } catch (IOException e) {
                        if (!(e instanceof SocketException)) {
                            Log.e(M.CPP, e.toString());
                        }
                    }
//                    try {
//                        outToServer.close();
//                    } catch (IOException e) {
//                        Log.e(M.CPP, e.toString());
//                    }
                }
            }.start();
            // current thread manages streams from server to client (DOWNLOAD)
            int bytes_read;
            try {
                while ((bytes_read = inFromServer.read(reply)) != -1) {
                    outToClient.write(reply, 0, bytes_read);
                    outToClient.flush();
                }
            } catch (IOException e) {
                Log.e(M.CPP, e.toString());
            } finally {
                try {
                    remoteSocket.close();
                } catch (IOException e) {
                    Log.e(M.CPP, e.toString());
                }
            }
            //outToClient.close();
            sClient.close();
        } catch (IOException e) {
            Log.e(M.CPP, e.toString());
        }
    }
Влад
  • 25
  • 5
  • You are closing the socket in both threads, so the thread that didn't close it first is getting this exception. Why are you surprised? NB It doesn't 'close another thread'. You are misquoting text that I have written here hundreds of times. It closes *the* other thread, i.e. if you close the input stream it closes the output stream, and *vice versa.* – user207421 Jul 27 '20 at 12:49
  • I commented out the closure of streams in general, as you can see in the code, but downloading files over 3Kb gives errors – Влад Jul 27 '20 at 13:46
  • I am talking about the code you posted iniitally. If you don't close the socket while it is still in use by another thread you can't possibly get this error. *Ergo* that is what you are still doing. You need to defer closing the socke until both threads have exited. – user207421 Jul 28 '20 at 02:31
  • If you could tell me how to do it it would be great – Влад Jul 29 '20 at 10:53

0 Answers0