DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2003

    How to get the user input while server is waiting for client's message

    I have a server/client program (using sockets)
    I used a thread to let a server always waiting for client's request, but how should I found that

    there is no message pass to server from client.
    public void run(){
    while (true){
    Socket server = serverP.accept();
    ObjectInputStream inFromClient = new ObjectInputStream(server.getInputStream());
    inMessage = (Message)inFromClient.readObject();
    System.out.println("Deadlock may occurred, please enter your command: ");
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    String command = inFromUser.readLine();
    The server is waiting for the inMessage that is passed from multiple clients. But if the server

    received no message after some time, it will assume something is wrong (eg, there is a

    deadlock), then it will ask the user to input the command to execute the method.

    If I put
    "System.out.println("Do you want to take Snapshot: ");
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    String command = inFromUser.readLine();"
    inside the while loop, it will keep asking user to enter the input when every time the client

    connect to the server. But I want to ask the user to enter the command only when the server

    can't get the response from clients.

    How should I do?
    Please help.

    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    If you look at the Socket class you will find that you can set them up so they will timeout if there is no activity. Place your message there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.