tokio udp multicast chat
1.0.0
This example implements a very simple CLI chat client which communicates over UDP multicast. It’s implemented with the tokio crate, an asynchronous runtime for writing event-driven, non-blocking applications with the Rust.
The code is trivial, but there is one interesting
detail to note. To allow communication between instances on the same host we need to enable
SO_REUSEADDR
for the UDP socket. The tokio API
doesn’t expose a direct way to do this, we instead use the
socket2 crate to construct a custom socket
that we upgrade to std::net::UdpSocket
and then into a tokio::net::UdpSocket
.
cargo
and Rust installed$ git clone https://github.com/henninglive/tokio-udp-multicast-chat/ && cd tokio-udp-multicast-chat
$ cargo build --release
(NOTE: There is a large performance differnce when compiling without optimizations, so I recommend alwasy using --release
to enable to them)target/release/tokio-udp-multicast-chat
$ cargo run --release
to build and then run, in one step