如何修复Android Tcp服务器

编程入门 行业动态 更新时间:2024-10-19 06:22:04
本文介绍了如何修复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服务器

本文发布于:2023-11-15 21:32:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   Android   Tcp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!