You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
use futures::select;
|
|
use futures::FutureExt;
|
|
|
|
use async_std::{
|
|
io::{stdin, BufReader},
|
|
net::{TcpStream, ToSocketAddrs},
|
|
prelude::*,
|
|
task,
|
|
};
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
|
|
|
pub(crate) fn main() -> Result<()> {
|
|
task::block_on(try_main("127.0.0.1:8080"))
|
|
}
|
|
|
|
async fn try_main(addr: impl ToSocketAddrs) -> Result<()> {
|
|
let stream = TcpStream::connect(addr).await?;
|
|
let (reader, mut writer) = (&stream, &stream);
|
|
let reader = BufReader::new(reader);
|
|
let mut lines_from_server = futures::StreamExt::fuse(reader.lines());
|
|
|
|
let stdin = BufReader::new(stdin());
|
|
let mut lines_from_stdin = futures::StreamExt::fuse(stdin.lines());
|
|
loop {
|
|
select! {
|
|
line = lines_from_server.next().fuse() => match line {
|
|
Some(line) => {
|
|
let line = line?;
|
|
println!("{}", line);
|
|
},
|
|
None => break,
|
|
},
|
|
line = lines_from_stdin.next().fuse() => match line {
|
|
Some(line) => {
|
|
let line = line?;
|
|
writer.write_all(line.as_bytes()).await?;
|
|
writer.write_all(b"\n").await?;
|
|
}
|
|
None => break,
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|