
[英]Cannot send a struct over channel: mpsc::Sender cannot be shared between threads safely
[英]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...
本文暫無回復,試試以下方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.