How can I get a TCP server to send a message back to the client? <p>I'm at a complete loss as to how I can get my server app to send a message back to the client after the client sent a message. My goal is the client sends a message say "Hello" to the server the server receives the message then sends back to the client "hello to you"</p> <p>my question How do set up the part of the server sending a message and the client receiving the message?</p> <p>my server</p> <pre><code>public class MainActivity extends Activity { private ServerSocket serverSocket; Handler updateConversationHandler; Thread serverThread = null; public String serverIP = "127.0.0.1"; private InetAddress serverAddr; public static final int SERVERPORT = 4444; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); updateConversationHandler = new Handler(); this.serverThread = new Thread(new ServerThread()); this.serverThread.start(); } class ServerThread implements Runnable { public void run() { Socket socket = null; try { serverSocket = new ServerSocket(SERVERPORT); } catch (IOException e) { e.printStackTrace(); } while (!Thread.currentThread().isInterrupted()) { try { socket = serverSocket.accept(); CommunicationThread commThread = new CommunicationThread(socket); new Thread(commThread).start(); } catch (IOException e) { e.printStackTrace(); } } } } class CommunicationThread implements Runnable { private Socket clientSocket; private BufferedReader input; public CommunicationThread(Socket clientSocket) { this.clientSocket = clientSocket; try { this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); } catch (IOException e) { e.printStackTrace(); } } public void run() { while (!Thread.currentThread().isInterrupted()) { try { String read = input.readLine(); updateConversationHandler.post(new updateUIThread(read)); } catch (IOException e) { e.printStackTrace(); } } } } class updateUIThread implements Runnable { private String msg; public updateUIThread(String str) { this.msg = str; } @Override public void run() { recieved.setText(""); if (msg != null){ recieved.setText(recieved.getText().toString()+"message recieved: "+ msg + “\n”); /* SEND MESSAGE */ } } } } </code></pre> <p>my client</p> <pre><code>public class MainActivity extends Activity { private Socket socket; private static final int SERVERPORT = 4444; public String serverIP= "127.0.0.1"; private InetAddress serverAddr; private Timer myTimer; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setServerIP(); setContentView(R.layout.main); new Thread(new ClientThread()).start(); } public void sendVerifiedMessage(byte message) throws IOException { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); out.println(message); /* receive message from client */ } public void run() { sendVerifiedMessage(“hello”); } } class ClientThread implements Runnable { @Override public void run() { try { InetAddress serverAddr = InetAddress.getByName(serverIP); socket = new Socket(serverAddr, SERVERPORT); connected = true; } catch (UnknownHostException e1) { e1.printStackTrace(); connected = false; } catch (IOException e1) { connected = false; e1.printStackTrace(); } } } } </code></pre>