0

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.

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).

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 ---> Here i wait for a message from the server but how can i wait in background without having my program freeze?
    }
}

}

2
  • its called threading. javas sockets are designed to block because you are supposed to run them in a different thread. in that thread you can wait and when soemthing comes you can return it to the ui (or console) by using some kind of eventlistener. in short: look up tutorials for multithreading in java Commented Sep 23, 2015 at 23:18
  • thanks for your response Commented Sep 24, 2015 at 10:43

1 Answer 1

3

The simplest way in Java is to listen for server data in a separate thread.

Make the input stream final and insert something like this before the while loop reading from the console:

new Thread() {
  public void run() {
    try {
      while (true) {
        System.out.println(dis.readUTF());
      }
    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}.start();

This creates an anonymous subclass of Thread, which will execute the run method in the background when start is called.

When your code grows, you may want to implement the Runnable interface in a separate class and hand it in to the Thread constructor instead.

Sign up to request clarification or add additional context in comments.

2 Comments

If this solves your problem, you may want to accept the answer (add the checkmark), so it doesn't remain open... Makes it easier to find questions that are still open...
just fyi (i don't think it really matters here): System.out.println(); is not thread safe. i can potentially kill the jvm if called from different threads (that actually happend to me). With threads, thread-safety is always a concern, reading up on it won't hurt (much ;) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.