1

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!

Darth.Vader
  • 5,079
  • 7
  • 50
  • 90
  • I am confused, the class is called FromBoard but you actually send data TO THE BOARD. You said you send plain 'A's, but you wrote __flg++__ which would send A, B, C, so on, eventually getting out of the ASCII range. ::::: Also the condition __flg != -99__ seems to be doomed, because you start from 65 up. – ilomambo May 28 '12 at 00:03
  • Thanks for taking the time and writing to me ilomambo! I was playing with my code while trying to debug it... hence the inconsistency. I have updated it now. Now it should just keep sending an 'A' indefinitely. Thanks! – Darth.Vader May 28 '12 at 00:34
  • In the stack trace you posted the exception is generated in FromBoard line 43, but line 43 of the code you posted is a comment. If you want us to have a serious look please post the real code you ran. Or repost the exception trace with the new messages. – ilomambo May 28 '12 at 05:44

1 Answers1

2

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

Community
  • 1
  • 1
Rhys
  • 2,807
  • 8
  • 46
  • 68