user2953119
user2953119

Reputation:

communication with Java server socket via telnet.exe

I'm writting a simple server for experimenting with server socket and telnet.exe

public static void main(String[] args) throws IOException {
    String line;
    ServerSocket ss = new ServerSocket(5555);
    Socket socket = ss.accept();
    System.out.println("Waiting for a client...");
    InputStream sin = socket.getInputStream();
    OutputStream sout = socket.getOutputStream();
    DataInputStream in = new DataInputStream(sin);
    DataOutputStream out = new DataOutputStream(sout);
    out.writeUTF("\u001B[2J");
    out.writeUTF("Hello client\r\n");
    line = in.readUTF();
    System.out.println("The dumb client just sent me this line : " + line);
    System.out.println("I'm sending it back...");
    out.writeUTF(line);
    out.flush();
    System.out.println("Waiting for the next line...");
    System.out.println();
}

Now I'm running this server and connecting to him via telnet.exe. It's ok. But when i'm sending message to server I dont receive this back:

enter image description here

Why it doesnt work?

Upvotes: 2

Views: 1973

Answers (2)

Philipp
Philipp

Reputation: 69703

A telnet client terminates each input-line with a newline. But a DataInputStream does not recognize this as a terminator for input-strings, because a DataInputStream is for binary data.

  1. Wrap your input stream with an InputStreamReader to handle it as a character-based input stream.
  2. Then wrap this one in a BufferedReader. This has the advantage that the input-stream can be filled by the socket in the background while your program executes. It also provides some handy utility methods like the following.
  3. Use the readLine method, which reads data until a newline is found.

Like this:

BufferedReader inputReader = new BufferedReader(new InputStreamReader(in));
[...]
line = inputReader.readLine();

For text-based output of newline-terminated messages back to the client, you should use the output analogue of the BufferedReader, which is the PrintWriter.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533790

DataInput is for reading binary.

From the Javadoc for readUTF()

Reads in a string that has been encoded using a modified UTF-8 format. The general contract of readUTF is that it reads a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.

First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . This integer value is called the UTF length and specifies the number of additional bytes to be read. These bytes are then converted to characters by considering them in groups. The length of each group is computed from the value of the first byte of the group. The byte following a group, if any, is the first byte of the next group.

This means the first two byte have to have the length in binary of the following UTF string.

You are typing something like k and j for the first two bytes so the length is something like 25000 bytes, i.e. you haven't typed that much which is why it doesn't return.


What you want instead is to be able to read/write text using classes like BufferedReader and PrintWriter.

Upvotes: 0

Related Questions