#Reading input from TcpStream

22 messages · Page 1 of 1 (latest)

proven saffron
#

I'm trying to create a user interface based on telnet (I know it's not secure and unencrypted), and when I press keys when focused on the telnet client, nothing really happens on the console, but I can see that the server sends "Hello World" after the handshake and telnet sending back packets to the server on key presses.

Can someone help me?
Thanks in advance

halcyon coral
#

Also, is your cliënt a terminal or a GUI?

proven saffron
#

My client is Windows built-in telnet client, on the cmd

halcyon coral
#

so where is the Rust code?

#

is it the telnet server?

proven saffron
#

Telnet is only the client

#

I will send the code

bitter remnant
#

Are you using telnet protocol on the rust server?

proven saffron
#

Telnet's protocol is just normal sockets, so I'm using a TcpListener

#
use log::*;
use std::io::{Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::time::Instant;

fn main() {
    log4rs::init_file("resources/log4rs.yml", Default::default()).expect("Can't init from file");
    info!("Listening on port 1240");

    let listener = TcpListener::bind("127.0.0.1:1240")
        .expect("Failed binding TCP listener to the provided address/port.");

    loop {
        match listener.accept() {
            Ok(mut stream) => handle_connection(&stream.1, &mut stream.0),
            Err(e) => error!("Connection failed! {}", e),
        }
    }
}

fn handle_connection(addr: &SocketAddr, stream: &mut TcpStream) {
    let i = Instant::now();
    info!("Handling connection on {}", addr);

    if let Err(err) = write!(stream, "Hello World!") {
        error!("Can't write to stream! {}", err)
    }

    loop {
        let mut buffer: Vec<u8> = Vec::new();
        match stream.read(&mut buffer) {
            Ok(size) => {
                if size > 0 {
                    info!("{:?}", buffer);
                    debug!("DATA RX");
                }
            }
            Err(err) => {
                error!("Failed to read from stream! {}", err);
                break;
            }
        }
    }

    debug!("Connection time: {:.2}", i.elapsed().as_secs_f32());
}
halcyon coral
#

like, it doesn’t implement the Telnet protocol

#

it’s just a TCP server

proven saffron
#

Because I get in the Telnet screen the "Hello world", which means the handshake was successful

halcyon coral
#

Yes, it is necessary to make sounds with your mouth in order to converse, but you can’t just make random sounds and expect to have a conversation

#

you need to adhere to a certain language (in this case, the Telnet protocol)

#

If you want to implement the Telnet protocol, read this document: https://datatracker.ietf.org/doc/html/rfc854

#

Or find an existing Rust library that implements a Telnet server

proven saffron