1

Hi guys I have read the answer to similar question but none of this could solve the problem im facing .

Step 1.) I make a ServerSocket (localhost port8675) , I accept the socket and get its corresponding OutputStream (I use async task for this)

step2.) I get chunks of data as byte[] (12kb).

step 3.) I write this 12kb byte[] on the outputstream.

The first chunk write is success but when i try and write the next chunk i face this error.

03-27 21:13:00.275 W/System.err(28866): java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)

03-27 21:13:00.275 W/System.err(28866): at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)

03-27 21:13:00.275 W/System.err(28866): at libcore.io.IoBridge.sendto(IoBridge.java:475)

03-27 21:13:00.275 W/System.err(28866): at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)

03-27 21:13:00.275 W/System.err(28866): at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)

03-27 21:13:00.275 W/System.err(28866): at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)

These are the code blocks Async task to make server socket and get output stream

        protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub


        try {
            providerSocket = new ServerSocket(8765);
            Log.d(TAG,"  ServerSocket made ");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,"ServerSocket failed ");
            e.printStackTrace();
        }

        //Wait for connection
        Log.d(TAG,"Waiting for connection");
        try {
            connection = providerSocket.accept();
            Log.d(TAG,"Connection received from " + connection.getInetAddress().getHostName());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,"ServerSocket accept failed ");
            e.printStackTrace();
        }


        try {
            dataOutputStream = connection.getOutputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            dataOutputStream.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }

Now to write the byte[] chunks i get

  Log.i(TAG, "incoming  data of length ="+data.length);
    new writetask().execute(data); 

protected Void doInBackground(byte[]... temp) {
    // TODO Auto-generated method stub

    if(temp[0] != null && (temp[0].length > 0 )){
        Log.d(TAG,"doInBackground of  server  socket start queued data len"+temp[0].length);
        mBuffer =   new byte[temp[0].length];
        mBuffer = temp[0];
       }                
       else 
       { Log.e(TAG,"empty buffer  enqued :(");
        return null ;
       }

        try {
            if(mBuffer != null)
            {
                Log.d(TAG, " buffer size = "+temp[0].length);
                Log.d(TAG," write buffer to socket");
                dataOutputStream.write(mBuffer,0,mBuffer.length);} // LINE NO 360.
            else
                Log.e(TAG, "no buffers  are recieved yet");
            mBuffer = null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            Log.d(TAG,"enqeueBuffer flush the socket");
            dataOutputStream.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.d(TAG,"doInBackground run end ");

Few points I have added only relevant code , The dataOutputStream is a classmember and socket accepting and outputstream writing are done in two seprate Async task.

these are the logs for the 1st data write which seems successful

03-27 21:13:00.150 : incoming data of length =12288

03-27 21:13:00.155 : doInBackground of server socket start

03-27 21:13:00.155 doInBackground of server socket start queued data len12288

03-27 21:13:00.155 buffer size = 12288

03-27 21:13:00.155 write buffer to socket 03-27 21:13:00.160 flush the socket

03-27 21:13:00.160 doInBackground run end

Now the second data write logs are as these

03-27 21:13:00.275 incoming data of length =12288

03-27 21:13:00.275 doInBackground of server socket start

03-27 21:13:00.275 doInBackground of server socket start queued data len12288

03-27 21:13:00.275 buffer size = 12288

03-27 21:13:00.275 write buffer to socket

03-27 21:13:00.275 W/System.err(28866): java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)

03-27 21:13:00.275 W/System.err(28866): at $writetask.doInBackground(SampleServiceImpl.java:360) // fails at write

what is the reason of this happening is it due to output stream needing to write chunks of data at fast interval (i doubt it as i have tested much faster write rate at local sockets.)

Kindly advise. thanks

amIT
  • 664
  • 13
  • 27

0 Answers0