jeudi 31 janvier 2019

Telnet server always returns garbage when connecting to it using Boost Asio

So I am trying to make a telnet client that connects to some address part for work and part for Boost::Asio learning purpose.

My small project has three handlers:

Resolve handler:

void resolverHandler(const boost::system::error_code& ec, ip::tcp::resolver::iterator iter) {
    if (ec) {
        cout << "Resolved failed with message: " << ec.message() << endl;
    }
    else {
        ip::tcp::endpoint endpoint = *iter;
        cout << "Connection to: " << endpoint.address() << ":" << endpoint.port() << endl;
        tcpSocket.async_connect(endpoint, connectHandler);
    }
}  

Connect handler

   void connectHandler(const boost::system::error_code& ec) {
        if (ec) {
            cout << "Connect failed with message: " << ec.message() << endl;
        }
        else {
            cout << "Connection established" << endl;
            tcpSocket.async_read_some(buffer(_data), readHandler);
        }
    }

Read handler:

void readHandler(const boost::system::error_code& ec, size_t ammountOfBytes) {
    if (ec) {
        cout << "Read failed with message: " << ec.message() << endl;
    }
    else {
        cout << ammountOfBytes << endl;
        cout << _data.data() << endl;
        tcpSocket.async_read_some(buffer(_data), readHandler);
    }
}

And this is my main function:

io_service ioservice;
ip::tcp::resolver resolver(ioservice);
ip::tcp::socket tcpSocket(ioservice);
array<char, 16> _data;
ip::tcp::resolver::query query("192.168.200.200", "23");


int main() {

    resolver.async_resolve(query, resolverHandler);

    ioservice.run();

    return 0;
}

But I always get garbage like this:

Connection to: 192.168.206.226:23
Connection established
15
 ² ²  ²# ²' ²$

I admit that I am new to telnet, but I am not sure why do I get this response ? Not sure if I need to null terminate the data that I receive before printing it, but even like that I have the same response.

Here is the normal response I should receive - tried with Windows Telnet:

Welcome Message (localhost) (ttyp0)

login:

Apreciate if someone has any ideas on what to do.

Aucun commentaire:

Enregistrer un commentaire