0

I'm trying to create a more advanced server-to-client program but I've stumbled onto a problem. I managed to ask the client to enter password, and if false it rejects the client.

However, I tried adding a while condition to the server: If client enters wrong password then ask him/her to re-enter password. But I tried using a scanner but don't think that's the solution as I receive errors.

Server side:

 while(pw.equals("PASS")){
        toClient.writeBytes("This pw " + pw + " is correct. Access granted! " + "\n");
        }
        toClient.writeBytes("Your password... " + pw + " is wrong. Please retry.");
        pw = fromClient.readLine();
        toClient.writeBytes(pw+ "\n");
   }

Client side:

    System.out.print("Enter your password: ");
        myPass = fromUser.readLine();

        toServer.writeBytes(myPass + "\n");

        myPass = fromServer.readLine();
        System.out.println(myPass);

The code is quite long. But everything works. The connection is set up, port number are the same. Only problem is I can't get the client to keep re-entering password until correct.

Do I have to make a while loop for client too? I'm not too sure...

2 Answers 2

1

1st: Why is there a while loop on the server side?

2nd: The loop using a flag on the client side as suggested by @j0chn is correct way to do this.

Sign up to request clarification or add additional context in comments.

6 Comments

I used a while loop because the server holds the correct password for the client. If client enters his password and doesn't matches what holds in the server then access denied. However, that's how I interpret it should work. Perhaps I'm wrong, can you elaborate on why if so? TY
Shouldn't the way it should work is by 'access denied'? When there is access denied, then the client loop should handle it and take care of it
I see where you're coming from! Basically, the server holds the password. The client contains the loop, and if the client sends the password that doesn't match, then it'll loop on until the password finally matches the server's password. If I want to decline service to the client if they entered password wrong. Would that still be in the client side, or implemented in the server's?
ofc on the client side. If this logic goes into the server consider what happens when there are like 10000 clients authenticating at same time? Scaling?
Thank you! Simple yet informative! I'll work on the client side.
|
0

I suggest a loop where you use a boolean to keep the loop alive.
Something like this:

boolean isCorrect = false;
System.out.print("Enter your password: "); 
while(!isCorrect){
    myPass = fromUser.readLine();

    toServer.writeBytes(myPass + "\n");

    if(myPass == fromServer.readLine()){
        isCorrect = true;
    }
    myPass = fromServer.readLine();

    System.out.println(myPass);
}

Maybe you have to change this part:

if(myPass == fromServer.readLine()){

to:

if(myPass.equals(fromServer.readLine()){

But remember that there are a lot of possibilities.

I hope it helps you.

7 Comments

I'd like to know why you downvoted my answer. So I might improve my next answers (or at least this one)!
Thanks for snappy reply. I'll try it out. I did not downvote, but I'll upvote you for your support :)
Sorry took me a while to get back to you. I finally managed to solve the problem, took me some trials & errors. Instead of using a boolean, I just used a while loop for the client. But it re-asks for password continuously now. TY once again!
If your loop still don't finish, would you pls post your new code. Than it is easier to comprehend your new problem :D
The loop finishes thanks. It'll end and send message if password is incorrect. I want to decline service now if password is wrong. I know the password method seems like it does that. But it only keeps asking the client to re-enter password. Do you think I should refuse server's functionality by implementing on the server class once the password received is incorrect? Or do it on the client class? Dem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.