我正在编写一个Android应用程序,通过TCP将数据从android发送到arduino板。出于测试目的,我只需将Android中的角色“A”写入Arduino。但是,我注意到写完20-30次后,我得到以下异常:
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)
这是从主活动调用的线程,通过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();
}
}
}
}
任何有关此方面的帮助都将受到高度赞赏。谢谢!