When I run this program, I enter the username as "dcole" and the password "test" but when I enter them they return with "ÿûÿû ÿûÿû'ÿýÿûÿýdcole" and "ÿþÿþ ÿþÿþ'ÿütest". Here’s the code:
clientoutput.write("Please enter your username: ".getBytes("UTF-8"));
clientoutput.flush();
String username = clientinput.readLine();
out("Client " + clientip + " logged in as " + username);
String askforpass = "Please enter the password for " + username + ": ";
clientoutput.write(askforpass.getBytes());
clientoutput.flush();
String password = clientinput.readLine();
out(password);
Console:
Client connected with the IP /127.0.0.1
Client /127.0.0.1 logged in as ÿûÿû ÿûÿû'ÿýÿûÿýdcole ÿþÿþ ÿþÿþ'ÿütest
If the input stream was pure UTF-8, then “dcole” and “test” would be unlikely to be garbled. (They would be fine if you used Latin-1 or ASCII to decode the bytes, and many other charsets as well.) Indeed you are seeing the expected characters, but with extra rubbish in front of it.
I suspect that the client is actually preceding the username and password with some extra bytes. In other words, it is not sending simple text – it is sending messages in some protocol that you haven’t described.
In fact, I think that this SO Question might explain your problem: Odd Behavior when Connecting to my Program