我正在建立windows pc和android aap之间的串行通信
这是我遵循的步骤 使用了portforwading方法,
执行命令提示符:
adb forward tcp:6666 tcp:6767
java代码行
try {
server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(6767));
server.setSoTimeout(TIMEOUT * 1000);
// attempt to accept a connection
client = server.accept();
Globals.socketIn = new Scanner(client.getInputStream());
Globals.socketOut = new PrintWriter(client.getOutputStream(),
true);
// Globals.socketIn.
} catch (SocketTimeoutException e) {
// print out TIMEOUT
e.printStackTrace();
connectionStatus = "Connection has timed out! Please try again";
mHandler.post(showConnectionStatus);
} catch (IOException e) {
Log.e(TAG, "" + e);
} finally {
// close the server socket
try {
if (server != null)
server.close();
} catch (IOException ec) {
Log.e(TAG, "Cannot close server socket" + ec);
}
}
但是获得超时异常。让我知道它有什么问题。
这是我的logcat
09-25 16:57:59.299: W/System.err(20521): java.net.SocketTimeoutException
09-25 16:57:59.299: W/System.err(20521): at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:108)
09-25 16:57:59.304: W/System.err(20521): at java.net.ServerSocket.implAccept(ServerSocket.java:202)
09-25 16:57:59.304: W/System.err(20521): at java.net.ServerSocket.accept(ServerSocket.java:127)
09-25 16:57:59.304: W/System.err(20521): at com.example.usbpccomminication.SocketMain$1.run(SocketMain.java:90)
09-25 16:57:59.304: W/System.err(20521): at java.lang.Thread.run(Thread.java:856)
09-25 16:57:59.309: W/System.err(20521): Caused by: libcore.io.ErrnoException: accept failed: EAGAIN (Try again)
09-25 16:57:59.309: W/System.err(20521): at libcore.io.Posix.accept(Native Method)
09-25 16:57:59.309: W/System.err(20521): at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:55)
09-25 16:57:59.309: W/System.err(20521): at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
09-25 16:57:59.314: W/System.err(20521): ... 4 more
答案 0 :(得分:0)
有效处理它的一种方法是定义连接超时,然后使用try catch块处理它。希望这会有所帮助。
HttpUrlConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30000); //set the timeout in milliseconds
//This is the average time interval
答案 1 :(得分:0)
因为我之前遇到过同样的问题。这根本不是编程问题。仅当App无法与服务器通信时才会发生此错误。 i,e客户端使用的网络与服务器连接的网络不同。 (很可能是wifi问题。重启wifi调制解调器可能会解决这个问题或电话)
答案 2 :(得分:-1)