No method 'write' for TcpStream

Hi,

I want to learn more about the language, and low level code.
So I have decided to implement SSH protocole following RFC.

But I have a problem...

Let's look at my code (you can get it on GitHub) :

In SshConnection struct, I have a 'write' method (I will maybe change the name), wich must send command through SSH.
So i have a TcpStream (got it from 'connect' method), and I am trying to use 'write' method on it.

Here the exemple I have found :

use std::io::prelude::*;
use std::net::TcpStream;

fn main() -> std::io::Result<()> {
    let mut stream = TcpStream::connect("127.0.0.1:34254")?;

    stream.write(&[1])?;
    stream.read(&mut [0; 128])?;
    Ok(())
} // the stream is closed here

But when i build my project, here the error I get :

error[E0599]: no method named `write` found for struct `TcpStream` in the current scope
    --> src\ssh\ssh_connection.rs:46:24
     |
46   |                 stream.write(ssh_packet.into_bytes());
     |                        ^^^^^ method not found in `TcpStream`
     | 
    ::: C:\Users\...\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\io\mod.rs:1291:8
     |
1291 |     fn write(&mut self, buf: &[u8]) -> Result<usize>;
     |        ----- the method is available for `Box<TcpStream>` here
     |
     = help: items from traits can only be used if the trait is in scope
     = note: the following trait is implemented but not in scope; perhaps add a `use` for it:
             `use std::io::Write;`

It said that the method is available for Box<TcpStream>. I have tried to put the stream into a box with Box::new(stream), but same error with 'the method is available for Box<Box<TcpStream>>'.

So I am lost...

Could you help me to understand what went wrong please ?

Does the code work when you apply compiler suggestion?

1 Like

Damn, I feel idiot... Thank you :slight_smile:

But now, another problem. I have a problem with borrow and mutable...

error[E0596]: cannot borrow `stream` as mutable, as it is not declared as mutable
  --> src\ssh\ssh_connection.rs:47:17
   |
47 |                 stream.write(ssh_packet.into_bytes());
   |                 ^^^^^^ cannot borrow as mutable
pub fn write(&mut self, command: String) {
    match &self.stream {
        Some(stream) => {
            let ssh_packet = SshPacket::new(command);
            stream.write(ssh_packet.into_bytes());
            // stream.read(&mut [0; 128]);
            todo!()
        }
        None => println!("You have to connect to the server before sending commands"),
    }
}

How to tell that local variable stream is mutable ?
I have tried to do Some(mut stream) => ... but I get another error, so it may be not that solution

Error for the correction try
error[E0507]: cannot move out of a shared reference
  --> src\ssh\ssh_connection.rs:44:15
   |
44 |         match &self.stream {
   |               ^^^^^^^^^^^^
45 |             Some(mut stream) => {
   |                  ----------
   |                  |
   |                  data moved here
   |                  move occurs because `stream` has type `TcpStream`, which does not implement the `Copy` trait

Do I miss something ?

Try doing match &mut self.stream {

1 Like

Great !
Thank you a lot for your help both of you :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.