Telnet Clients and Their Treatment of EOL

2019-07-25 09:05发布

问题:

This is a rather convoluted question, and for that I apologize. I wrote a Linux C sockets application, a basic framework for a simplistic chat server. The server is running on my laptop. The client is Telnet at the moment until I write a designated client application (that'll be more secure, hopefully). There are better applications for sending generic network data from a client end, I know, but I got interested about why a certain thing happens on one Telnet client but not another.

The first Telnet client test was on another Linux laptop. It works as expected. The next, however, was a Blackberry app called BBSSH that allows Telnet and SSH connections. I went via the Telnet option, and it works too. Except, it doesn't exactly.

The server code does the usual read call to retrieve a block of data, which gets treated as a string, i.e. a message. The former client reads until I hit enter, and then it sends one string of characters. The BB app, however, sends every single character as if I've been pressing enter after each of them, which I haven't. Obviously this is something to do with buffering, what certain clients class as a EOL from the user input, etc. I just can't pinpoint it.

To illustrate, here is the server outputting messages it's received from the clients.

First, the message from the Linux client:

client name: this is a test

Now, for BBSSH:

client name: t
client name: h
client name: i
client name: s
client name:
client name: i
client name: s
client name:
client name: a
client name:
client name: t
client name: e
client name: s
client name: t

Any help?

回答1:

Telnet clients can operate in line-mode or character-mode. The BBSSH client seems to be operating in character-mode for some reason.

It's possible that your server could force the client into line-mode by sending the client an instruction to that effect during the negotiation that takes place at the start of a telnet connection.

The byte sequence your server would need to send to the client is 0x255 0x253 0x34, which translates as "Interpret As Command, Do, Linemode". If the client is willing/able to operate in line-mode, it should reply with 0x255 0x251 0x34 ("Interpret As Command, Will, Linemode").

If this is all new to you (i.e. your telnet server currently doesn't do any negotiation at all), google for terms like "telnet negotiation" or take a look at some of the relevant RFCS (RFC 854 is Telnet itself, RFC 1184 covers the Linemode option).



回答2:

TCP is stream-orientated, and so there's no such thing as a "message". You might receive all the data together, or just a fragment of it at a time.

In your case, you might want to buffer anything received until you hit an EOL marker.