mirror of https://github.com/stjepang/smol
52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
//! A TCP chat client.
|
|
//!
|
|
//! First start a server:
|
|
//!
|
|
//! ```
|
|
//! cargo run --example chat-server
|
|
//! ```
|
|
//!
|
|
//! Then start clients:
|
|
//!
|
|
//! ```
|
|
//! cargo run --example chat-client
|
|
//! ```
|
|
|
|
use std::net::TcpStream;
|
|
|
|
use smol::{future, io, Async, Unblock};
|
|
|
|
fn main() -> io::Result<()> {
|
|
smol::block_on(async {
|
|
// Connect to the server and create async stdin and stdout.
|
|
let stream = Async::<TcpStream>::connect(([127, 0, 0, 1], 6000)).await?;
|
|
let stdin = Unblock::new(std::io::stdin());
|
|
let mut stdout = Unblock::new(std::io::stdout());
|
|
|
|
// Intro messages.
|
|
println!("Connected to {}", stream.get_ref().peer_addr()?);
|
|
println!("My nickname: {}", stream.get_ref().local_addr()?);
|
|
println!("Type a message and hit enter!\n");
|
|
|
|
let reader = &stream;
|
|
let mut writer = &stream;
|
|
|
|
// Wait until the standard input is closed or the connection is closed.
|
|
future::race(
|
|
async {
|
|
let res = io::copy(stdin, &mut writer).await;
|
|
println!("Quit!");
|
|
res
|
|
},
|
|
async {
|
|
let res = io::copy(reader, &mut stdout).await;
|
|
println!("Server disconnected!");
|
|
res
|
|
},
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
})
|
|
}
|