SLMT / telnet-rs

A simple implementation of Telnet in Rust.
MIT License
45 stars 20 forks source link

How to write buffer after reading? #3

Closed amaury1093 closed 6 years ago

amaury1093 commented 6 years ago

I'm creating a telnet connection to google's smtp server.

extern crate telnet;

use telnet::{Telnet, TelnetEvent};
use std::str::from_utf8;

pub fn connect() {
    let mut connection = Telnet::connect(("gmail-smtp-in.l.google.com.", 25), 256)
        .expect("Couldn't connect to the server...");

    loop {
        let event = connection.read().expect("Read Error");
        match event {
            TelnetEvent::Data(read_buffer) => {
                // Debug: print the data buffer
                println!("{}", from_utf8(&read_buffer).unwrap());

                // Buffer to write to telnet
                let write_buffer = "HELO Hi".as_bytes(); // TODO Define buffer depending on read_buffer
                connection
                    .write(&write_buffer)
                    .expect("Error while writing");
            }
            _ => {}
        }
    }
}

I wish to write a telnet message after I get some data. I.e. If I receive this data, then I write that message.

However, here I am writing "HELO Hi", with no response.

SLMT commented 6 years ago

Hello @amaurymartiny ! Sorry for answering your question so late. I was so busy this month.

I have looked into your problem and found the source. Basically, you use TelnetConnection in the correct way. The problem is that you missed the newline character (\n) in your write_buffer. So, you can fix the problem by simply adding a \n at the end of the buffer:

let write_buffer = "HELO Hi\n".as_bytes();

This should be enough to make the program run. If you encounter more problems, please let me know.

BTW, I still found some other problems in my code when I was finding the problem. For example, I think that it lacks a flush() to ensure that the connection sends the data we write. Thanks for giving me a chance to review my code.

amaury1093 commented 6 years ago

Thanks, that solved it!