DenisM
DenisM

Reputation: 223

How write characters to TelnetOutputStream one at a time?

I am using apache.commons.net.telnet.

I have char[] array. I am calling TelnetClient.getOutputStream().write(array[i]).

I expected that my data will be sent by one character at a time, but Wireshark shows that first character is sent alone, and remaining characters are sent together. Why do I get this situation and how can I send my data character by character?

Upvotes: 0

Views: 446

Answers (3)

Gee
Gee

Reputation: 26

Have you tried calling flush() after each call to write()?

flush() function should send anything in the stream before the buffer is filled.

Upvotes: 0

MTilsted
MTilsted

Reputation: 5543

OK. Here is the correct answer. To send the data as fast as possible, do the following:

Call setTcpNoDelay(true) on your output stream.

Call write with your entire array. You should NEWER EVER write one byte at a time. Writing the entire array at the same time will be much faster.

Call flush() after your write.

This is the fastest way to send the data, and it is also the way which will create the least latency. That is: Sending the bytes one at a time will NOT ensure that the server will receive them any faster.

Sending the data one byte at a time will slow you down, not speed things up.

Upvotes: 1

MTilsted
MTilsted

Reputation: 5543

If you really want to send the chars one at a time(Why, it will really slow you down) I guess you have to iterate over the array and call write with a single byte each time. And then flush after each write. Something like (Not tested, but it should give you the a hint).

OutputStream os=TelnetClient.getOutputStream();
for(int i=0;i!=array.length;i++) {
  os.write(array[i]);
  os.flush();
}

Upvotes: 0

Related Questions