本文介绍了如何修复Android Tcp服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void runTcpServer() { ServerSocket ss = null; try { ss = new ServerSocket(TCP_SERVER_PORT); //ss.setSoTimeout(10000); //accept connections Socket s = ss.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); //receive a message String incomingMsg = in.readLine() + System.getProperty("line.separator"); Log.i("TcpServer", "received: " + incomingMsg); text.append("received: " + incomingMsg); //send a message String outgoingMsg = "goodbye from port " + TCP_SERVER_PORT + System.getProperty("line.separator"); out.write(outgoingMsg); out.flush(); Log.i("TcpServer", "sent: " + outgoingMsg); text.append("sent: " + outgoingMsg); //SystemClock.sleep(5000); s.close(); } catch (InterruptedIOException e) { //if timeout occurs e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }
i尝试运行程序运行的runTcp方法直到套接字s = ss.accept(); 然后应用程序崩溃 我该如何解决这个问题?
i try to run the runTcp methods the program work until Socket s = ss.accept(); then the application crashed How can i fix this?
推荐答案在主线程上执行不允许的网络操作。有关详细信息: developer.android/reference/ android / os / NetworkOnMainThreadException.html [ ^ ] http:/ /android-er.blogspot.nl/2012/04/androidosnetworkonmainthreadexception.html [ ^ ] 祝你好运! You perform a networking operation on the main thread which is not allowed. For more details: developer.android/reference/android/os/NetworkOnMainThreadException.html[^] android-er.blogspot.nl/2012/04/androidosnetworkonmainthreadexception.html[^] Good luck!
更多推荐
如何修复Android Tcp服务器
发布评论