Search code examples
javauser-inputbufferedreadertelnet

Buffered Reader returns unexpected character from user input


I have a multi-chat server client program and I am attempting to get input from a client in a telnet putty window. The prompt:

String login = "2-Enter Username and a password:";
        clientoutput.write((login).getBytes());

The user input: enter image description here

This is read by a BufferedReader:

 BufferedReader br = new BufferedReader(new InputStreamReader(clientInput));
        String inputLine;
        String returnMessage;
        while ((inputLine = br.readLine()) != null) {

            // Split input line with space as delimiter using import jar
            String[] words = StringUtils.split(inputLine);

            // Ensure we don't get a null pointer
            if (words != null && words.length > 0) {
                String command = words[0];

            if ("logoff".equalsIgnoreCase(command) || "q".equalsIgnoreCase(command) || "quit".equalsIgnoreCase(command)) {
                logOff();
                break;
                }else 
                    // Log user in
                    try {
                        clientLogIn(clientoutput, words);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }

However the first word of user input is consistently read as

ÿû

: Eclipse console output:

User ÿû has logged in

So my question is where is this character ÿû coming from and is there a work around?

I am using WIndows 10 and Eclipse Version: 2019-03 (4.11.0) Build id: 20190314-1200

Additional info: I've tried to capture the user input and directly print to console :

if (login.contains("ÿû")) {
            login = login.substring(1);
            System.out.println("New login after removal of unxepected char: " + login);
        } else {
            System.out.println("User eneterd login : " + login);

        }

Output:

User entered login : -Enter Username and a password:

User ÿû has logged in // after the first word has been taken


Solution

  • Since you're actually writing your own protocol, the problem seems to be that you're using the wrong client to test your server.

    That is, telnet is not meant as a general TCP client, as it sends commands alongside the actual data you're trying to send. Also, it works on bytes, not java strings (which you noticed when you tried to check for those weird chars at the beginning of the string).

    You may avoid the problem completely if you used something like netcat to test your code instead.