hardartcore
hardartcore

Reputation: 17037

Android AsyncTask send/read data using Socket

I'm working on a test project, something like a basic chat program using wi-fi connection. I'm creating sockets to connect two different devices, but my problem is that when I send the first message, it's showing in the other device. But if I try to send again, I can see in the logs from the first one, that the message is sent, but it never shows up in the second device.

I've tried to implement the reading of the received data in another thread..or in Async Task, but the problem is still there. Here are both ways of my implementation :

Single Thread :

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

And here is 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(String); -

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

Any ideas how to fix this issue?

And another problem is that when I am reading the received data, the first letter of the sent string never shows. It always starts from the second letter.

Thanks in advance.

Upvotes: 1

Views: 6230

Answers (1)

Tuna Karakasoglu
Tuna Karakasoglu

Reputation: 1267

If i were you i will try to implement serverSocket = new ServerSocket(DNSUtils.port); only once with new; not in every thread.

Upvotes: 1

Related Questions