To enforce clients to turn off local echo and disable line buffering (enabling character-at-a-time mode), your telnet server needs to send specific IAC (Interpret As Command) sequences during the initial connection handshake.
Here are the essential handshake elements:
Turn Off Local Echo
Send: IAC WILL ECHO (bytes: 255, 251, 1)
- This tells the client that the server will handle echo
- The client should respond with IAC DO ECHO and disable local echo
Turn Off Line Buffering (Character-at-a-Time Mode)
Send: IAC DO SGA (bytes: 255, 253, 3)
- Requests the client to suppress go-ahead signals
- Helps establish character-at-a-time transmission
Send: IAC WILL SGA (bytes: 255, 251, 3)
- Indicates the server will also suppress go-ahead
- Both sides suppressing GA enables full character-at-a-time mode
Example Implementation (Python)
import socket
# Telnet command bytes
IAC = 255 # Interpret As Command
WILL = 251 # I will use option
DO = 253 # Please use option
ECHO = 1 # Echo option
SGA = 3 # Suppress Go-Ahead option
def send_initial_handshake(client_socket):
# Server will handle echo (client turns off local echo)
client_socket.send(bytes([IAC, WILL, ECHO]))
# Enable character-at-a-time mode
client_socket.send(bytes([IAC, DO, SGA]))
client_socket.send(bytes([IAC, WILL, SGA]))
Additional Considerations
Some clients may require additional negotiation:
- IAC DONT LINEMODE (255, 254, 34) - Explicitly disable linemode if the client supports it
- Handle client responses appropriately (IAC DO/DONT, IAC WILL/WONT)
- Some clients may send their own option requests that need acknowledgment
The key is that these commands should be sent immediately after accepting the connection, before any actual data exchange begins. Different telnet clients may respond slightly differently, but this combination works with most standard implementations.