For some reason, I want to create a server for the standard Telnet application as client to connect to. I wrote a basic TCP server in C++, its purpose is to serve an interactive menu, so I have to set ECHO on my side.
When the client connects, I send IAC WILL ECHO to it, and I expected to get IAC DO ECHO. To my surprise, what I get is IAC DO ECHO, IAC WON'T ECHO, IAC DON'T ECHO. Three messages one by one. And the client is still echoing by itself.
What am I doing wrong? What do those three messages mean?
int main() {
TcpListener tcpListener(10001); // custom tcp helper library
auto stream = tcpListener.acceptClient();
vector<uint8_t> msg = {255, 251, 1}; // send IAC WILL ECHO on start
stream.writeBytes(msg);
auto message = stream.readBytes(100);
while (!message.empty()) {
for (auto c : message) cout << (int)c << endl; // to see what i'm getting from client
cout << "length: " << message.size() << "\n";
stream.writeBytes(message); // echo back
message = stream.readBytes(100);
}
return 0;
}
I think my mistake can be now seen in my code, but I worked on some more complicated version of it. The problem is I echoed all messages from client, so when I sent IAC WILL ECHO it responded with IAC DO ECHO. When I echoed it client said it won't do it and then sent IAC DON'T ECHO (to clear the situation I think). So of course to avoid such errors first thing in telnet server is to properly handle negotation.