繁体   English   中英

无法发送到我的 mpsc 无界通道,收到 Err SendError

[英]Cannot send to my mpsc unbounded channel, getting a Err SendError

我正在尝试从我的 tcp 连接中获取输出值,然后简单地将其排队到我的无界通道中。

我似乎收到了一个发送错误(代码截图下方显示的错误)。

有什么我做错了吗?

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {

    #[derive(Debug)]
    struct Login {
        account: String,
        login: String,
        password: String
    }
    

    #[derive(Debug)]
    enum TcpCommand {
        Login,
        Incomding(String)
    }

    let (tx, mut rx) = mpsc::unbounded_channel::<TcpCommand>();

    tokio::spawn(async move {
        loop {
            let s = rx.blocking_recv();
            println!("received {:?}", s)
        }
    });


    let stream = TcpStream::connect("123.321.11.1:9800").await?;
    

    loop {
        let ready = stream.ready(Interest::READABLE | Interest::WRITABLE).await?;

        if ready.is_readable() {
            let mut data = vec![0; 1024];
            
            match stream.try_read(&mut data) {
                Ok(n) => {
                    println!("read {} bytes", n);
                    
                    match str::from_utf8(&data) {
                        Ok(v) => {
                        let res = tx.send(TcpCommand::Incomding(v[..n].to_string()));
                        println!("result res: {:?}", res);
                        println!("result: {}", v);
                        }
                        ,
                        Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
                    };                
                   

                }
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
                    continue;
                }
                Err(e) => {
                    //return Err(e.into());
                    println!("error: {}", e);
                }
            }

        }        
    }
}

我在尝试发送到我的频道时遇到错误:

 read 29 bytes result res: Err(SendError(Incomding("Please login...\r\n")) result: Please login...
①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM