Android的TCP客户端 [英] Android TCP client

查看:160
本文介绍了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的或者如果你有一个活动的参考可以使用<一个href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)"相对=nofollow> 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客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆