Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Accepting user Input for (Java)

Options
  • 04-12-2007 12:14pm
    #1
    Registered Users Posts: 3,803 ✭✭✭


    Hey,

    I need a bit of help :D

    I have a server and a client. The client contains a menu which accepts user input and calls the required method on the server based on the user input.

    The client looks something like this:
    do
    {
        System.out.println("Type:\n 1 to login\n 2 to logoff\n 3 to send a message");
        command = Integer.parseInt(br.readLine()); // where br is the Bufferedreader
    
       if (command = 1)
          call login method on server etc
       ..............
    }
    while (command != -1);
    

    The code is just a rough basoic example of what im doing. However, at certain times, the server will send a message to the client by calling a method on the client. This message will require a response from the client that will be sent back to the server (response is taken using a BufferedReader as well).

    Here is where the problem lies. The server can send the message (call the method on the client) at any time. This means that the when the client responds to the server, the response is actually been taking as a command in the client (in the while loop shown above).

    I hope I havent confused you :D Is there a way to overcome this problem?

    Thanks in advance.


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    if (command = 1)
    

    That should be
    if (command == 1)
    

    The other will always equate to true.

    As for you question look into event handlers.
    http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    I'm struggling to understand your problem. Basing on your pseudo code and what you have said you seem to be struggling in code to differentiate between a clients command (a number like 1, 2, 3) and a different input as a response to a servers query. Is this the case?

    You might have to show a more fleshed out example to get any help on this one. Event handlers as Hobbes has said is probably what you are after. Having all the logic in the while loop looks a bit messy.


  • Registered Users Posts: 3,803 ✭✭✭Benzino


    I'm struggling to understand your problem. Basing on your pseudo code and what you have said you seem to be struggling in code to differentiate between a clients command (a number like 1, 2, 3) and a different input as a response to a servers query. Is this the case?

    Yes, thats excalty the problem. Here some code i hope will better explain it.

    Client:
    public static void main(String args[])
    {	
    	InputStreamReader is = new InputStreamReader(System.in);
    	BufferedReader keyboard = new BufferedReader(is); // where is is an inputStream
    	int command;
    	do
    	{
    		System.out.println("Enter: \n1 - to login\n2 - to logoff\n3 - to send a message");
    		try
    		{
    			command = Integer.parseInt(keyboard.readLine());
    		}
    		catch (Exception e)
    		{
    			command = 0;
    		}
    
    		if (command == 1)
    			server.login();
    		else if (command == 2)
    			server.logoff();
    		else
    			server.sendMessage();
    	}(while command != -1);
    }
    
    public static boolean recievedMsg()
    {
    	System.out.println("You have received a message from another client, Would you like to accept it? (y/n)");
    	InputStreamReader is = new InputStreamReader(System.in);
    	BufferedReader response = new BufferedReader(is); // where is is an inputStream
    	
    	if (response.equalsIgnoreCase("y") )
    		return true;
    	else
    		return false;
    }
    
    public static printMessage(String msg)
    {
    	System.out.println(msg);
    }
    

    Server:
    public static void login()
    {
    	......
    }
    
    public static void logoff()
    {
    	......
    }
    
    public static void sendMessage()
    {
    	if( recipient.recievedMsg() )
    		recipient.printMessage();
    }
    

    So for example, on clientSide, the system is waiting for the client 1 to enter a command (1 to login etc). But if another client sends a message to the client 1, it will call client 1's recievedMessage() function.This method requires the user to enter input, however in the do while (in the main method), it is also waiting for the user to enter in a response.

    If the user enters a response, it is read as a command first (bufferedReader in while loop). The next input entered by the user is then taken as a reponse to the server (the bufferedReader in recievedMessage() ).

    What I want is to overwrite the bufferedReader in the while loop temporarily, or even pause it until the user responds to the server.

    P.S: I know using 2 BufferedReaders doesnt work and isn't wise, but I just used them to illustrate what I want to do. Thanks for your replies by the way.


Advertisement