1

我正在做一个测试项目,比如一个使用 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);
    }
}

更改文本(字符串);-

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);
        }
    });
}

任何想法如何解决这个问题?

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

提前致谢。

4

1 回答 1

1

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

于 2012-07-30T15:44:33.120 回答