I made this program for a school, and it involves a telnet server. When I run it, it returnes strange characters such as ÿûÿû. Here is the output:
Client connected with the IP /127.0.0.1
Client /127.0.0.1 has entered the username ÿûÿû ÿûÿû'ÿýÿûÿýdcole.
My code:
Socket client = serv.accept();
InetAddress clientip = client.getInetAddress();
out("Client connected with the IP " + clientip);
InputStream clientinput = client.getInputStream();
OutputStream clientoutput = client.getOutputStream();
Scanner in = new Scanner(clientinput);
clientoutput.write("Please enter your username: ".getBytes());
String username = in.nextLine();
out("Client " + clientip + " has entered the username " + username + ".");
String pass = "Please enter the password for " + username + ": ";
clientoutput.write(pass.getBytes());
String password = in.nextLine();
if (username.equals("dcole") && password.equals("test")) {
clientoutput.write("\r\nCorrect password!".getBytes());
} else {
clientoutput.write("\r\nIncorrect password!".getBytes());
}
Maybe there is some dirty information in your
InputStreamfrom when you created theSocket. Are you able to clean out the data before you ask for the username, such as callingin.nextLine();and discarding the data, or by callingclientinput.skip(clientinput.available());Try doing this before asking for the username – ie immediately after creating theScanner.As a follow-on, as you are talking to a Telnet server, I would definately encourage you to follow the advise of @StephenC – you should be implementing the correct handshaking and data transmission properties of the Telnet specification rather than simply sending Strings through the Socket.