title: Java socket programming. How can i wait for server response in the background?
body: <p>I am trying to build a client/server program and it works fine. When i send a message it gets shown on the server and then it waits for a response from the server but i cant do anything while i wait for a response.    </p>
  
  <p>My question is : how can i wait for a response in the background while i can still send messages and just show the server message when it is sent(if it is sent).</p>
  
  <pre><code>import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Scanner;
  
  
  public class Client {
  public static void main(String[] args) throws IOException,UnknownHostException{
  
      // Specify remote address and port
      Socket cs = new Socket("localhost", 1337);
      // Input and Output
      DataInputStream dis = new DataInputStream(cs.getInputStream());
      DataOutputStream dos = new DataOutputStream(cs.getOutputStream());
  
      // Writing message to the server
      String message = null;
      Scanner scan = new Scanner(System.in);
  
      while(true){
          message = scan.nextLine();
          if(message.equals("exit")) System.exit(0);
          dos.writeUTF(message);
          dos.flush();
          // Check for messages from server ---&gt; Here i wait for a message from the server but how can i wait in background without having my program freeze?
      }
  }
  </code></pre>
  
  <p>}</p>