When you enter fewer characters in the EditText field, than it was before it simply adds them, i.e. if at the first input you enter ttt, and at the second s, then the output will be stt. Help make sure that the field is somehow cleaned, or it can somehow clean the buffer. Another attached a couple of screenshots of the work.
File MainActivity.java:
package ru.atbattle.chasie.socetclient; import android.net.Uri; import android.os.AsyncTask; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.telecom.DisconnectCause; import android.text.Editable; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; //import com.google.android.gms.appindexing.Appindex; //import com.google.android.gms.common.spi.GoogleApiClient; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.io. *; import java.net. *; import java.lang. *; import java.util. *; public class MainActivity extends AppCompatActivity { EditText editText, editText3, editText2; Button button, button2, button3, button4; Spinner spinner; Socket s; String IPn, str, str1; InputStream is; OutputStream os; byte buf1[] = new byte[512]; byte buf2[] = new byte[512]; int len1; Thread th; AsyncTask<Void, Integer, String> readMsg; AsyncTask<String, Integer, Void> writeMsg; AsyncTask<String, Integer, Boolean> connect; AsyncTask<String, Integer, Boolean> disconnect; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ // private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) this.findViewById(R.id.editText); editText2 = (EditText) this.findViewById(R.id.editText2); editText3 = (EditText) this.findViewById(R.id.editText3); button = (Button) this.findViewById(R.id.button); spinner = (Spinner) this.findViewById(R.id.spinner); } public void getConnection(View view) { connect = new AsyncTask<String, Integer, Boolean>() { @Override protected Boolean doInBackground(String... params) { try { s = new Socket(params[0], 9999); is = s.getInputStream(); os = s.getOutputStream(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } @Override protected void onPostExecute(Boolean param) { editText3.setText(((param)?"Connected!":"Error!")); } }; connect.execute(spinner.getSelectedItem().toString()); } public void getDisconnection(View view) { disconnect = new AsyncTask<String, Integer, Boolean>() { @Override protected Boolean doInBackground(String... params) { try { s.close(); is.close(); os.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } @Override protected void onPostExecute(Boolean param) { editText3.setText(((param)?"Disconnected!":"Error!")); } }; disconnect.execute(); } public void SendReceive(View view) { writeMsg = new AsyncTask<String, Integer, Void>() { @Override protected Void doInBackground(String... params) { try { os.write(params[0].getBytes(), 0, params[0].length()); os.flush(); } catch (IOException e) { e.printStackTrace(); } return null; } }; writeMsg.execute(new String[] { editText.getText().toString() }); } public void Accept(View view) { readMsg = new AsyncTask<Void, Integer, String>() { @Override protected void onPreExecute() { editText2.setText(null); } @Override protected String doInBackground(Void... params) { try { int bytes = is.read(buf1, 0, 512); os.flush(); } catch(IOException e) { e.printStackTrace(); return e.getMessage(); } return new String(buf1, 0); } @Override protected void onPostExecute(String message) { editText2.setText(message); } }; readMsg.execute(); } @Override public void onStart() { super.onStart(); } @Override public void onStop() { super.onStop(); } }

