Darth.Vader
Darth.Vader

Reputation: 6291

Android Connection reset by peer exception

I am writing an Android app to send data from android to arduino board over TCP. For testing purposes I just write the character 'A' from Android to Arduino. However, I noticed that after writing 20-30 times I get the following exception:

W/System.err(11561): java.net.SocketException: Broken pipe
W/System.err(11561):    at org.apache.harmony.luni.platform.OSNetworkSystem.write(Native Method)
W/System.err(11561):    at dalvik.system.BlockGuard$WrappedNetworkSystem.write(BlockGuard.java:284)
W/System.err(11561):    at org.apache.harmony.luni.net.PlainSocketImpl.write(PlainSocketImpl.java:472)
W/System.err(11561):    at org.apache.harmony.luni.net.SocketOutputStream.write(SocketOutputStream.java:68)
W/System.err(11561):    at io.raas.FromBoard.run(FromBoard.java:43)
W/System.err(11561):    at java.lang.Thread.run(Thread.java:1019)

Here is the Thread which is called from the main activity to send data over tcp:

/*
 * Cleanup: SERVERPORT should be in Constants.java
 */
package io.raas;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.util.Log;

    class ToBoard implements Runnable {
        Socket s = null;
        static ServerSocket ss = null;
        InputStream i = null;
        OutputStream o = null;
        public static final int SERVERPORT = 6000;
        char flg = 65;
        int count = 0;
        public void run() {
            try {
                if(ss==null) {  ss = new ServerSocket(SERVERPORT);
                s = ss.accept(); }
            } catch(Exception e) {e.printStackTrace();}

            // change this to while true....
            while (true) {
                Log.d("SEPERATOR", "-----------------------------------------------------------------------------");
                count++ ;
                try {
                    if(flg == 65) {
                        o = s.getOutputStream();
                        Thread.sleep(500);
                        o.write(flg);
                        o.flush();
                    }
                    /*
                    i = s.getInputStream();
                    int intRead = i.read();
                    */
                    /*
                    if(i==null) {
                        i = s.getInputStream();
                    }
                    int intChar = (char)i.read();
                    Log.d("WRITING_TO_BOARD", "====================================> READ FROM BOARD: " + intChar);
                    */
                } catch (Exception e) {
                    Log.d("TX WE HAVE A PROBLEM", "YEA WE DO!");
                    e.printStackTrace();
                }
            }
        }

    }

Any help with this will be highly appreciated. Thanks!

Upvotes: 1

Views: 2952

Answers (1)

Rhys
Rhys

Reputation: 2877

I believe this is caused when you are writing to a connection that the other end has already closed.

There is a another question with details below:

Broken Pipe Exception

Upvotes: 2

Related Questions