Android的TCP客户端

编程入门 行业动态 更新时间:2024-10-22 13:48:47
本文介绍了Android的TCP客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前工作的Andr​​oid TCP客户端上。 我想我的Andr​​oid设备连接到TCP服务器在我的电脑上,并接收一旦数据每2秒钟。问题是,我得到,因为我在TCP客户端已经实现了while循环强制关闭在我的应用程序。 我试着写以不同的方式循环,这将使TCP客户端检查服务器插槽,但没有成功。如何才能使一个循环,将检查服务器套接字没有得到该部队密切?

I'm currently working on a tcp client in Android. I want to connect my android device to a tcp server on my computer and receive the data once every 2 seconds. The problem is that I'm getting force close on my application because of the while loop that I've implemented in the tcp client. I've tried writing in different ways the loop that will make the tcp client checking the server socket, but with no success. How can make a loop that will check the server socket without getting the force close?

下面是我目前使用我的code:

Here's my code that I'm currently using:

public class Connection implements Runnable { @Override public void run() { try { sk=new Socket(server,port); viewsurface.setText("connected"); flag = true; } catch (UnknownHostException e) { viewsurface.setText("failed 1 socket"); flag = false; } catch (IOException e) { viewsurface.setText("failed 2 socket"); flag = false; } while (flag == true){ try { checkin = sk.getInputStream(); checkint = checkin.available(); if (checkint > 0){ try { BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream())); received = in.readLine(); viewsurface.setText(received); } catch (IOException e) { viewsurface.setText("failed to receive"); } } Thread.sleep(2000); } catch (IOException e) { viewsurface.setText("checkin failed"); } catch (InterruptedException e) { e.printStackTrace(); } } }

}

推荐答案

您需要粘贴你得到使部队密切例外,之前任何人都可以提供得体的帮助。

You need to paste the exception that you are getting to cause the force close, before anyone can provide decent help.

但是,这可能会解决这个问题提出了一些建议。

But some suggestions that might solve the problem.

  • 最有可能是问题,viewText.setText只能叫从UI线程。有相当多的方法来处理这​​个问题。您可以使用 AsyncTask的或者如果你有一个活动的参考可以使用 runOnUIThread 并传递一个可运行调用的setText。
  • 将签= sk.getInputStream();到循环之前。我们没有理由通过循环得到中sTREM每个周期。
  • 请不要通过循环创建的BufferedReader每个周期。在循环之前将它
  • .sleep(2000)并不能保证完全2秒。
  • Most likely to be the problem, viewText.setText can only be called from the UI thread. There's quite a few ways to handle this. You can use AsyncTask or if you have an Activity reference you can use runOnUIThread and pass in a runnable that calls setText.
  • Move checkin = sk.getInputStream(); to before the loop. There's no reason to get the strem every cycle through the loop.
  • Do not create the BufferedReader every cycle through the loop. Move it before the loop
  • .sleep(2000) does not guarantee exactly 2 seconds.

我有一些code格式问题,所以我很抱歉。

I'm having some code formatting issues so I apologize.

private class DownloadFilesTask extends AsyncTask<Void, String, Void> { protected Long doInBackground(Void... nothing) { try { sk=new Socket(server,port); publishProgress("connected"); flag = true; } catch (UnknownHostException e) { publishProgress("failed 1 socket"); flag = false; } catch (IOException e) { publishProgress("failed 2 socket"); flag = false; } while (flag == true){ try { checkin = sk.getInputStream(); checkint = checkin.available(); if (checkint > 0){ try { BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream())); received = in.readLine(); publishProgress(received); } catch (IOException e) { publishProgress("failed to receive"); } } Thread.sleep(2000); } catch (IOException e) { updateProgress( } catch (InterruptedException e) { e.printStackTrace(); } return; } protected void onProgressUpdate(String... progress) { viewsurface.setText(progress[0]); } protected void onPostExecute(Void result) { //nothing } }

更多推荐

Android的TCP客户端

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

发布评论

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

>www.elefans.com

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