Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connection reset when trying to read inputStream

I want to read a inputstream from a socket into a byteArray , but i get the follow error:

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)

I don't udnerstand why because i don 't close my socket until i finish to read the inputStream

try {
            in = connexion.getSocket().getInputStream();
            out = connexion.getSocket().getOutputStream();

            byte[] buffer= new byte[2048] ;          
            ByteArrayOutputStream baos= new ByteArrayOutputStream();
            int read = 0;
            while((read = in.read(buffer)) > 0) // exception thrown here
            {
                baos.write(buffer, 0, read);
            }
            reception = baos.toByteArray();


        } 

        catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            try{
                in.close();
                out.close();
                connexion.getSocket().close();
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        }

Server side:

 public static void main(String[] args) throws Exception {
        ServerSocket s = new ServerSocket(port,2);
        Socket soc ;

        while(true){
        soc = s.accept(); } }

Thank you very much

like image 459
ulquiorra Avatar asked May 03 '26 22:05

ulquiorra


1 Answers

It seems that the connection has been closed by the server before reading. This could be an issue with the request you are sending or an issue at their end.

like image 96
Lo Juego Avatar answered May 05 '26 12:05

Lo Juego



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!