3

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

ulquiorra
  • 931
  • 4
  • 19
  • 39
  • possible duplicate of [java.net.SocketException: Connection reset](http://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset) – Peter Lawrey Aug 10 '12 at 10:56
  • @Peter Lawrey In my case i implement my own server so it's different from the topic you mentionned. – ulquiorra Aug 10 '12 at 11:13
  • 1
    The reasons for the error are the same, i.e. its a problem at the other end. See EJP's answer. – Peter Lawrey Aug 10 '12 at 11:40

3 Answers3

2

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.

Lo Juego
  • 1,305
  • 11
  • 12
  • Thank you very much , i edited my code with the server side and like u can see , the connection is never close , so i don t understant the error – ulquiorra Aug 10 '12 at 12:00
  • @user902509 The connection is closed when 'soc' gets garbage-collected. The server code here doesn't resemble anything in real life. Fix it to do something real before you spend any more time on the client side of the problem. – user207421 Aug 11 '12 at 03:31
0

Resolved I set a little timeout before retrieve inputstream and it works great now Thank you all

ulquiorra
  • 931
  • 4
  • 19
  • 39
  • 1
    The real problem here is the strange server code that just accepts connections and then throws them away. Fix that first. – user207421 Aug 11 '12 at 03:29
0

I see that you've already solved this issue, but I recently had a similar situation. What I found that is actually causing this problem is the array's size of you byte array fo reading. I you try to read more data than is the socket the ConnectionReset is trown, try with a size of 1.

Saludos!