Results 1 to 2 of 2
  1. #1
    noahssite is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Not detecting "exit" in client

    Hello, I wrote two applications, one a server and one a client. Below is my client. They react properly when the client sends text to the server (the server responds with "The command was received") however when the user types "exit" the server responds with "The command was received" when the socket should have closed and the while loop exited.

    Here is my client code:

    Java Code:
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
    				System.in));
    		Socket clientSocket = new Socket("localhost", 7001);
    		DataOutputStream outToServer = new DataOutputStream(
    				clientSocket.getOutputStream());
    		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
    				clientSocket.getInputStream()));
    		
    		outToServer.writeBytes("ACK\n");
    		System.out.println(inFromServer.readLine());
    
    		String userCommand = inFromUser.readLine();
    
    		while (userCommand != "exit") {
    			outToServer.writeBytes(userCommand + "\n");
    			System.out.println(inFromServer.readLine());
    			
    			userCommand = inFromUser.readLine();
    		}
    
    		System.out.println("User typed 'exit', exiting...");
    		
    		clientSocket.close();
    I debuged my code and userCommand did equal "exit" however it still continued with the loop. Why?

    Thanks,
    Noah

  2. #2
    noahssite is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    RESOLVED. The error was that I needed to use !userCommand.equals() instead of !=.

Similar Threads

  1. Replies: 6
    Last Post: 05-18-2010, 06:11 PM
  2. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 09:32 PM
  3. Replies: 1
    Last Post: 04-02-2009, 10:50 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Replies: 6
    Last Post: 08-18-2008, 07:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •