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
pub fn with_multicast_local_ipv4(self, multicast_local_ipv4: Ipv4Addr) -> Self
pub fn with_multicast_local_port(self, multicast_local_port: u16) -> Self
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
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for MulticastSocket
impl Debug for MulticastSocket
Auto Trait Implementations§
impl Freeze for MulticastSocket
impl RefUnwindSafe for MulticastSocket
impl Send for MulticastSocket
impl Sync for MulticastSocket
impl Unpin for MulticastSocket
impl UnwindSafe for MulticastSocket
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more