Method not found I can't sort out

Good day all. I have been chasing my tail on this for a few hours and need some help. I have lifted the echo server example from the tokio documentation. As is it works correctly and echoes the received data back to the sender on the TcpListener port. I am trying to change the echo section to send the data out to a different device on a different port. I have been in a game of "fix this, break that". I am now at a point where the compiler is telling me that it cannot find the method tokio::TcpStream.write_all(). The code I am using is lifted straight from the tokio documentation. I have tried both the TcpStream and TcpSocket examples. Any help would be appreciated. Thanks everybody.

use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::net::TcpSocket

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
//Receive print data from POS    
    let listener = TcpListener::bind("192.168.0.49:9101").await?;
    loop {
        let (mut socket, _) = listener.accept().await?;
        tokio::spawn(async move {
            let mut buf = [0; 8192];
            // In a loop, read data from the socket and write the data back.
            loop {
                let n = match socket.read(&mut buf).await {
                    // socket closed
                    Ok(n) if n == 0 => return,
                    Ok(n) => n,
                    Err(e) => {
                        println!("failed to read from socket; err = {:?}", e);
                        return;
                    }
                };
//Format the data 
// Write the new data to the printer
                let mut stream = TcpStream::connect("192.168.0.62:9100").await;
                stream.write_all(&buf[0..n]).await; 
                /*let addr = "192.168.0.62:9100".parse().unwrap();
                let printer_socket = TcpSocket::new_v4();
                let stream = printer_socket.connect(addr).await; 
                
                if let Err(e) = stream.write_all(&buf[0..n]).await {
                    println!("failed to write to socket; err = {:?}", e);
                    return;
                } */
                 /*if let Err(e) = socket.write_all(&buf[0..n]).await {
                    println!("failed to write to socket; err = {:?}", e);
                    return; 
                } */
            }
        });
    }
}

stream example

use tokio::net::TcpStream;
use tokio::io::AsyncWriteExt;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let mut stream = TcpStream::connect("127.0.0.1:8080").await?;

    // Write some data.
    stream.write_all(b"hello world!").await?;

    Ok(())
}

socket example

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    let stream = socket.connect(addr).await?;

    Ok(())
}

That's weird because I'm seeing this in the documentation:

@H2CO3 Here is teh complete error

TcpStream::connect returns a Result<TcpStream>, not a TcpStream, so you need to unwrap it or otherwise handle that error case (similarly AsyncWriteExt::write_all returns a Result that you should handle)

1 Like

@RustyYato AHA! Now the error makes sense. Thank you sir.