简体   繁体   中英

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

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

I am trying to take the ouput values from my tcp connection, and simply queue that into my unbounded channel.

I seem to be getting a send error (error shown below the code snipped).

Is there something I am doing wrong?

#[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);
                }
            }

        }        
    }
}

I am getting an error while trying to send to my channel:

 read 29 bytes result res: Err(SendError(Incomding("Please login...\r\n")) result: Please login...
No answers.You can refer to the related questions on the right.
暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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