letresult = stream.write(b"hello world\n").await; println!("wrote to stream; success={:?}", result.is_ok()); }
3. 运行
准备
Windows
1
telnet127.0.0.16142
Mac
1
nc -l 127.0.0.16142
运行代码
1
cargo run
如果出错,则运行
1
RUST_BACKTRACE=full cargo run
可以查看到出错的堆栈
总结
用tokio实现出一个客户端程序 实现功能:
连接到本地6142端口
发送字符串hello world\n
检测是否发送成功并打印
可能错误: 本地端口监听失败或还未监听,会出现错误:
1 2
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 61, kind: ConnectionRefused, message: "Connection refused" }', src/main.rs:6:22 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace