Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Boolean Value Not Changing

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Boolean Value Not Changing

    Im writing a simple UDP Server, UDP Client program. The client sends a packet to the server containing, "login:username:password". The server splits the string up, delimited by colons, and checks to see if the username and password are valid. If they are, it responds with "Valid", if not, it responds with "Invalid". In the client program, there is a while that will continue to ask the user for username and password until "Valid" is received from the server. However, I have a boolean in the client named success. It is initialized to false, and when the server replies with "Valid", success should be changed to true. For some reason, I cannot get success to change to true on a "Valid" response form the server. Any ideas?

    Client:
    class UDPClient 
    { 
        public static void main(String args[]) throws Exception 
        { 
    		String serverIP;
    		String username;
    		String password;
    		String out;
    		boolean success = false;
     
    		System.out.println("\nProgrammer: Lucas Byerley");	
     
    		//New BufferedReader object to read input from user
    		BufferedReader inFromUser = 
    		new BufferedReader(new InputStreamReader(System.in));
     
    		//get server IP from client
    		System.out.print("\nEnter Server IP: ");
    		serverIP = inFromUser.readLine();
     
    		//establish the socket
    		DatagramSocket clientSocket = new DatagramSocket(); 
     
    		//connect to server
    		InetAddress IPAddress = InetAddress.getByName(serverIP); 
     
    		//create the send and receive packets
    		byte[] sendData = new byte[1024]; 
    		byte[] receiveData = new byte[1024];
     
    		//Ask for username and password until the server
    		//responds with "Success"
    		while(!(success))
    		{
    		//Get username from user
    		System.out.print("Username: ");
            username = inFromUser.readLine(); 
    		//Get password from user
    		System.out.print("Password: ");
    		password = inFromUser.readLine();
    		//combine username and password into 1 string, colon delimted
    		out = "login:" + username + ":" + password + ":";
     
    		//attach the login information to send packet
    		sendData = out.getBytes();
     
    		//New DatagramPacket object, send packet
    		DatagramPacket sendPacket =
    		new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
     
    		//Send the packet
    		clientSocket.send(sendPacket);
     
    		//New DatagramPacket object, recieve packet
    		DatagramPacket receivePacket =
    		new DatagramPacket(receiveData, receiveData.length);
     
    		//receive the packet
    		clientSocket.receive(receivePacket);
     
    		//Display response
    		String response =
    		new String(receivePacket.getData());
     
    		System.out.println("FROM SERVER: " + response);
     
    		if(response.equals("Valid"))
    			success = true;
    		System.out.println(success);
     
    		}
    		//close the socket
    		clientSocket.close();
    		}}

    Server:
    class UDPServer 
    { 
      public static void main(String args[]) throws Exception 
        { 
    		String user1 = "ALICE";
    		String user2 = "BOB";
    		String user3 = "TED";
    		String password = "123";
    		String answer;
     
    		System.out.println("\nProgrammer: Lucas Byerley");
     
    		//establish the socket
    		DatagramSocket serverSocket = new DatagramSocket(9876); 
     
    		//initialize the send and receive packets
    		byte[] receiveData = new byte[1024]; 
    		byte[] sendData  = new byte[1024]; 
     
    		while(true) 
            { 
     
    			//DatagramPacket object, the packet received
    			DatagramPacket receivePacket = 
                new DatagramPacket(receiveData, receiveData.length); 
    			serverSocket.receive(receivePacket); 
     
    			//recieve the login info
    			String sentence = new String(receivePacket.getData()); 
    			//receive clients IP address
    			InetAddress IPAddress = receivePacket.getAddress(); 
    			//receive clients port number
    			int port = receivePacket.getPort(); 
     
    			//split the string up, colon delimeted. token[0] should equal "login",
    			//token[1] should be clients username, token[2] clients password
    			String[] tokens = sentence.split(":");
     
    			//look at first token of string to see if user is logging
    			//in or needs a list
    			if(tokens[0].equals("login"))
    			{
    				//check for correct username and password
    				if((tokens[1].toUpperCase().equals(user1) || 
    					tokens[1].toUpperCase().equals(user2) ||
    					tokens[1].toUpperCase().equals(user3)) && (tokens[2].equals(password)))
    					{
    						answer = "Valid\n";
    						//compile and send the packet
    						sendData = answer.getBytes();
    						DatagramPacket sendPacket = 
    						 new DatagramPacket(sendData, sendData.length, IPAddress, port);
    						serverSocket.send(sendPacket);
    					}
    					else
    					{
    						answer = "Invalid\n";
    						//compile and send the packet
    						sendData = answer.getBytes();
    						DatagramPacket sendPacket = 
    						 new DatagramPacket(sendData, sendData.length, IPAddress, port);
    						serverSocket.send(sendPacket);
    					}
    			}
    			else if(tokens[0].equals("list"))
    			{
    				answer = "Here is your list.";
     
    				//compile and send the packet
    				sendData = answer.getBytes();
    				DatagramPacket sendPacket = 
    				 new DatagramPacket(sendData, sendData.length, IPAddress, port);
    				serverSocket.send(sendPacket);
     
    			}
    }}}
    Last edited by bosox960; April 20th, 2010 at 10:34 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Boolean Value Not Changing

    Hi, maybe because your server responde Valid\n or Invalid\n and your client is wainting Valid without \n.
    Can u try?
    Bye

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Value Not Changing

    Yes, I have tried changing the value in the if statement to response.equals("Valid\n") but still could not get the boolean to change to true.

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Value Not Changing

    Fixed! Apparently using response.startsWith() instead of response.equals() fixes the problem.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Boolean Value Not Changing

    Quote Originally Posted by bosox960 View Post
    Fixed! Apparently using response.startsWith() instead of response.equals() fixes the problem.
    Congratulations on solving that!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Help with Java MenuItem and Boolean
    By Just Incredible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 8th, 2010, 03:15 PM
  2. ArrayList Boolean Issue
    By kite98765 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2010, 07:15 AM
  3. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM
  4. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM
  5. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM