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:
Code :
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:
Code :
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);
}
}}}
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
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.
Re: Boolean Value Not Changing
Fixed! Apparently using response.startsWith() instead of response.equals() fixes the problem.
Re: Boolean Value Not Changing
Quote:
Originally Posted by
bosox960
Fixed! Apparently using response.startsWith() instead of response.equals() fixes the problem.
:D Congratulations on solving that!