0

I'm trying to create a server to send a message which is based on the calculation result from another class.

My question is that since the calculation result is from another class, how do I make the server pause till the result is ready, and how should I pass the result to my server class and send out the result?

public class MyServer {
    ServerSocket providerSocket;
    Socket connection = null;
    static ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    public static String myServerSend;

    BufferedReader data = new BufferedReader(data);

    MyServer() {}

    void run() {
        try {
        providerSocket = new ServerSocket(2013, 10);
        System.out.println("Waiting for connection");

        connection = providerSocket.accept();

        out = new ObjectOutputStream(connection.getOutputStream());
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());

        do {
            try {
                message = (String) in.readObject();
                System.out.println("server receive>" + message);


                // HERE IS MY QUESTION
                // myServerSend is the result from other class, 
                                    //How can I pause the server here till myServerSend is ready???????
                sendMessage(myServerSend);


            } catch (ClassNotFoundException classnot) {
                System.err.println("Data received in unknown format");
            }
        } while (!message.equals("bye"));

    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

}


    //write msg into ObjectOutputStream
public static void sendMessage(String msg) {
    try {
        out.writeObject(msg);
        out.flush();
        System.out.println("server send>" + msg);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}
Kon
  • 10,702
  • 6
  • 41
  • 58
Shen
  • 2,911
  • 3
  • 15
  • 10

3 Answers3

0

Use

    Thread.sleep(30000); // Milli secs - 30 secs -- Put your sleep time
sendMessage(myServerSend);
NPKR
  • 5,368
  • 4
  • 31
  • 48
0

Without more specific info about what you have tried and why you have discarded what you have tried, I see several options here.

  1. Call directly the other class and wait till the result is ready. This may not be a good idea if the calculation takes too long, but if not, it's the simplest way.

  2. You can apply polling and get the server to sleep for a certain amount of time to not exhaust resources while waiting for an answer.

  3. Use synchronized objects and concurrency via wait and notify methods. Some useful links on this: 1 2 3

Community
  • 1
  • 1
Gothmog
  • 871
  • 1
  • 8
  • 20
0

You have few options to acheive this:

1- Create a Thread for your calculation and call join to make your server wait for the thread to finish

Thread thread = new Thread() {
   public void run(){
      // Call your calculation class
   }
}
thread.start();

thread.join(); // surround with try and catch

// or you can use to timeout if the calculation took long
// thread.join(MAX_TIME_MILLIS);

sendMessage(myServerSend);

2- Use wait/notify on a shared object between your server and calculation class

3- Use semaphore object initialized with 0 and call acquire in your server class to wait and call release after you finish your calculations, see my answer here for an example

Community
  • 1
  • 1
iTech
  • 18,192
  • 4
  • 57
  • 80