74 lines
2.2 KiB
Rust
74 lines
2.2 KiB
Rust
use std::{net::SocketAddr, path::Path, time::Duration};
|
|
|
|
use s2n_quic::{Client, Server, client::Connect};
|
|
|
|
const SERVER_ADDRESS: &str = "127.0.0.1:2021";
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
println!("Hello, world!");
|
|
|
|
tokio::spawn(server());
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
client().await;
|
|
}
|
|
|
|
async fn server() {
|
|
let mut server = Server::builder()
|
|
.with_io(SERVER_ADDRESS)
|
|
.unwrap()
|
|
.with_tls((
|
|
Path::new("certificates/cert.pem"),
|
|
Path::new("certificates/key.pem"),
|
|
))
|
|
.unwrap()
|
|
.start()
|
|
.unwrap();
|
|
while let Some(mut connection) = server.accept().await {
|
|
println!(
|
|
"Server Name = {}",
|
|
connection.server_name().unwrap().unwrap().to_string()
|
|
);
|
|
while let Ok(Some(mut stream)) = connection.accept_bidirectional_stream().await {
|
|
println!(
|
|
"{}",
|
|
String::from_utf8(stream.receive().await.unwrap().unwrap().to_vec()).unwrap()
|
|
);
|
|
stream
|
|
.send("Hi from Tahinli too".as_bytes().into())
|
|
.await
|
|
.unwrap();
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn client() {
|
|
let client = Client::builder()
|
|
.with_io("127.0.0.1:0")
|
|
.unwrap()
|
|
.with_tls(Path::new("certificates/cert.pem"))
|
|
.unwrap()
|
|
.start()
|
|
.unwrap();
|
|
|
|
println!("Client Address = {}", client.local_addr().unwrap());
|
|
let connect =
|
|
Connect::new(SERVER_ADDRESS.parse::<SocketAddr>().unwrap()).with_server_name("localhost");
|
|
let mut connection = match client.connect(connect).await {
|
|
Ok(connection) => connection,
|
|
Err(err_val) => {
|
|
eprintln!("Client Connection | {}", err_val);
|
|
return;
|
|
}
|
|
};
|
|
connection.keep_alive(true).unwrap();
|
|
|
|
let stream = connection.open_bidirectional_stream().await.unwrap();
|
|
let (mut receive_stream, mut send_stream) = stream.split();
|
|
send_stream
|
|
.send("Hi from Tahinli".as_bytes().into())
|
|
.await
|
|
.unwrap();
|
|
let received = receive_stream.receive().await.unwrap().unwrap();
|
|
println!("{}", String::from_utf8(received.to_vec()).unwrap());
|
|
}
|