title: Android AsyncTask send/read data using Socket
body: <p>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.</p>
  
  <p>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 :</p>
  
  <p>Single Thread : </p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>And here is AsyncTask :</p>
  
  <pre><code>class ServerTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
      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);
      }
  }
  </code></pre>
  
  <p>changeText(String); -</p>
  
  <pre><code>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);
          }
      });
  }
  </code></pre>
  
  <p>Any ideas how to fix this issue?</p>
  
  <p>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.</p>
  
  <p>Thanks in advance.</p>