Create async packet-based servers with ease, Built with gamedev in mind. Stay tuned for UDP support, and more!
```rust use packetz::{server::, packet::};
async fn main() -> Result<(), std::io::Error> {
let server: Server<&str> = Server::bind("0.0.0.0:5515"); // The extra &str is the type of the argumet in server::bind()
, so that server::bind is able to be a const fn.
let listener: ServerListener = server.listen().await?;
loop {
let (
mut connection,
addr
) = listener.accept().await?;
let _: tokio::task::JoinHandle<Result<(), std::io::Error>> = tokio::spawn(async move {
'l: loop { // In a real world scenario we would check for errors on the `send` and `recv` methods, and break the loop if one is found, and disconnect the client without disconnecting all other clients.
let msg = connection.recv().await?;
connection.send(msg.body).await?;
connection.disconnect(); // This is optional, as all it does is drop the PacketStream, and breaking the loop should automaticall drop it.
break 'l;
}
Ok(())
});
}
} ```
```rust use packetz::{client::, packet::};
async fn main() -> Result<(), std::io::Error> { let mut client = packetz::client::connect("0.0.0.0:5515").await?; client.send(b"Hello, Packetz!").await?; println!("{}", String::from_utf8( client.recv().await? )?); Ok(()) } ```
Dependencies for these examples:
toml
[dependencies]
packetz = "0.1.0" # Replace this with the latest version, if it's not already the latest version.
tokio = { version = "1.29.0", features = ["net", "io-util", "time", "rt", "rt-multi-thread"] }