Android AsyncTask使用Socket发送/读取数据

时间:2012-07-30 15:28:11

标签: android sockets android-asynctask

我正在开展测试项目,比如使用Wi-Fi连接的基本聊天程序。 我正在创建连接两个不同设备的套接字,但我的问题是,当我发送第一条消息时,它会在另一台设备中显示。但是,如果我再次尝试发送,我可以在第一个日志中看到消息已发送,但它从未显示在第二个设备中。

我试图在另一个线程中实现读取接收到的数据..或者在异步任务中,但问题仍然存在。以下是我实施的两种方式:

单线程:

public void listenForSocket(){
    thread =  new Thread(new Runnable() {
        public void run() {
            Log.e("READDATAFROMSOCKET","READDATAFROMSOCKET");
            try {
                // sets the service running state to true so we can get it's state from other classes and functions.                     
                serverSocket = new ServerSocket(DNSUtils.port);
                client = serverSocket.accept();
                client.setKeepAlive(true);
                InputStream is = client.getInputStream();
                Log.d("","is Size : "+is.available());
                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                int readed = in.read();
                Log.d("","readed bytes : "+readed);
                String line = "";
                while ((line = in.readLine()) != null) {
                    Log.i("","line : "+line);
                    changeText(line);
                }

                //client.close();
                //serverSocket.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

这是AsyncTask:

class ServerTask extends AsyncTask<Void, Void, Void>{

    private String line = "";

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Log.e("ASYNCTASK","ASYNCTASK");
            // sets the service running state to true so we can get it's state from other classes and functions.                     
            serverSocket = new ServerSocket(DNSUtils.port);
            client = serverSocket.accept();
            client.setKeepAlive(true);
            InputStream is = client.getInputStream();

            Log.d("","is Size : "+is.available());
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            int readed = in.read();
            Log.d("","readed bytes : "+readed);

            while ((line = in.readLine()) != null) {
                Log.i("","line : "+line);                   
            }

            //client.close();
            //serverSocket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        changeText(line);
    }
}

changeText(字符串); -

private void changeText(final String line) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            LinearLayout.LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.RIGHT;
            TextView sendMsg = new TextView(MainActivity.this);
            sendMsg.setText(DNSUtils.clientName+" : "+line);
            sendMsg.setTextColor(Color.DKGRAY);
            sendMsg.setTextSize(18);
            layout.addView(sendMsg, params);
        }
    });
}

如何解决此问题?

另一个问题是,当我读取收到的数据时,发送的字符串的第一个字母永远不会显示。它总是从第二个字母开始。

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果我是你,我将尝试用新的serverSocket = new ServerSocket(DNSUtils.port);实施一次;不在每个线程中。