以下代码每秒调用一次。它似乎第一次工作但在使用java.netConnectException ETIMEDOUT的后续调用时失败。 接收设备是MicroChip WiFi模块,可能只有一个端口。
private void sendData() {
new Thread() {
@Override
public void run() {
try {
Socket socket = null;
DataOutputStream dataOut = null;
try {
if (socket == null) {
socket = new Socket("1.2.3.4", 2000);
}
dataOut = new DataOutputStream(socket.getOutputStream());
dataOut.writeUTF(dataString());
L.d("send");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
L.d("Skt close");
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (dataOut != null) {
try {
dataOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (Exception e) {
Log.d("Exception in thread", e.getStackTrace() + "");
}
}
}.start();
}