pub struct MulticastSocket { /* private fields */ }Expand description
A builder for creating multicast UDP sockets suitable for mDNS.
MulticastSocket provides a convenient way to create properly configured
UDP sockets for mDNS communication. The resulting socket will be:
- Bound to the specified address (typically
0.0.0.0:5353) - Configured with
SO_REUSEADDRenabled - Configured with
SO_REUSEPORTenabled (on supported platforms) - Set to non-blocking mode for async compatibility
- Joined to the mDNS multicast group (224.0.0.251)
§Examples
Basic usage with tokio:
use rtc_mdns::MulticastSocket;
use std::net::SocketAddr;
let bind_addr: SocketAddr = "0.0.0.0:5353".parse().unwrap();
let std_socket = MulticastSocket::new(bind_addr).into_std()?;
let socket = tokio::net::UdpSocket::from_std(std_socket)?;With a specific network interface:
use rtc_mdns::MulticastSocket;
use std::net::{Ipv4Addr, SocketAddr};
let bind_addr: SocketAddr = "0.0.0.0:5353".parse().unwrap();
let interface = Ipv4Addr::new(192, 168, 1, 100);
let std_socket = MulticastSocket::new(bind_addr)
.with_interface(interface)
.into_std()?;Implementations§
Source§impl MulticastSocket
impl MulticastSocket
Sourcepub fn with_multicast_local_ipv4(self, multicast_local_ipv4: Ipv4Addr) -> Self
pub fn with_multicast_local_ipv4(self, multicast_local_ipv4: Ipv4Addr) -> Self
Sets the local IPv4 address to bind the multicast socket to.
Defaults to unspecified (0.0.0.0), letting the OS choose.
Sourcepub fn with_multicast_local_port(self, multicast_local_port: u16) -> Self
pub fn with_multicast_local_port(self, multicast_local_port: u16) -> Self
Sets the local port to bind the multicast socket to.
Defaults to the mDNS port (5353), which is required to receive multicast queries from other hosts; a different port is only useful for testing.
Sourcepub fn with_interface(self, interface: Ipv4Addr) -> Self
pub fn with_interface(self, interface: Ipv4Addr) -> Self
Sets a specific network interface for multicast operations.
If not set, the socket joins the multicast group on all interfaces
(INADDR_ANY).
§Arguments
interface- The IPv4 address of the network interface to use.
§Example
use rtc_mdns::MulticastSocket;
use std::net::Ipv4Addr;
let builder = MulticastSocket::new()
.with_interface(Ipv4Addr::new(192, 168, 1, 100));Sourcepub fn into_std(self) -> Result<UdpSocket>
pub fn into_std(self) -> Result<UdpSocket>
Converts this builder into a configured std::net::UdpSocket.
This method creates the socket with the following configuration:
SO_REUSEADDRenabled (allows multiple processes to bind)SO_REUSEPORTenabled on Unix platforms (except Solaris/illumos)- Non-blocking mode enabled (for async compatibility)
- Joined to the mDNS multicast group (224.0.0.251)
§Errors
Returns an error if:
- Socket creation fails
- Setting socket options fails
- Binding to the address fails
- Joining the multicast group fails
§Example
use rtc_mdns::MulticastSocket;
use std::net::SocketAddr;
let bind_addr: SocketAddr = "0.0.0.0:5353".parse().unwrap();
let std_socket = MulticastSocket::new(bind_addr).into_std()?;
// Use with tokio:
let socket = tokio::net::UdpSocket::from_std(std_socket)?;§Platform Notes
- On Unix-like systems (except Solaris/illumos),
SO_REUSEPORTis enabled to allow multiple processes to bind to the same port.
Trait Implementations§
Source§impl Clone for MulticastSocket
impl Clone for MulticastSocket
Source§fn clone(&self) -> MulticastSocket
fn clone(&self) -> MulticastSocket
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more