pub struct UdpSocket { /* private fields */ }Expand description
A UDP socket
Implementations§
Source§impl UdpSocket
impl UdpSocket
Sourcepub fn default_builder_for(local: &SocketAddr) -> Result<UdpBuilder, Error>
pub fn default_builder_for(local: &SocketAddr) -> Result<UdpBuilder, Error>
Create builder with default options, but doesn’t bind yet.
To create a UdpSocket from a builder go through the std::net::UdpSocket created by
builder.bind(...)?.
Sourcepub fn from_std(
stream: UdpSocket,
handle: LazyHandle,
) -> Result<UdpSocket, Error>
pub fn from_std( stream: UdpSocket, handle: LazyHandle, ) -> Result<UdpSocket, Error>
Wraps an already bound tcp stream
Sourcepub fn from_mio(
stream: UdpSocket,
handle: LazyHandle,
) -> Result<UdpSocket, Error>
pub fn from_mio( stream: UdpSocket, handle: LazyHandle, ) -> Result<UdpSocket, Error>
Wraps an already bound tcp stream
Sourcepub fn bind_port(port: u16) -> Result<UdpSocket, Error>
pub fn bind_port(port: u16) -> Result<UdpSocket, Error>
Binds a new UDP socket to IPv6 [::] with V6_ONLY=false (i.e. also listen on IPv4) and the
given port.
If the port is 0 the OS will select a random port.
Sourcepub fn bind_ipv4_port(port: u16) -> Result<UdpSocket, Error>
pub fn bind_ipv4_port(port: u16) -> Result<UdpSocket, Error>
Binds a new UDP socket to IPv4 0.0.0.0 and the given port.
If the port is 0 the OS will select a random port.
Sourcepub fn bind(local: SocketAddr) -> Result<UdpSocket, Error>
pub fn bind(local: SocketAddr) -> Result<UdpSocket, Error>
Bind a new UDP socket to the specified address.
Sourcepub fn bind_with(
local: SocketAddr,
handle: LazyHandle,
) -> Result<UdpSocket, Error>
pub fn bind_with( local: SocketAddr, handle: LazyHandle, ) -> Result<UdpSocket, Error>
Bind a new UDP socket to the specified address.
Sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the local socket address of this socket.
Sourcepub fn try_clone(&self) -> Result<UdpSocket, Error>
pub fn try_clone(&self) -> Result<UdpSocket, Error>
Creates a new independently owned handle to the underlying socket.
The new listener isn’t registered to a reactor yet.
Sourcepub fn try_clone_with(&self, handle: LazyHandle) -> Result<UdpSocket, Error>
pub fn try_clone_with(&self, handle: LazyHandle) -> Result<UdpSocket, Error>
Creates a new independently owned handle to the underlying socket.
Sourcepub fn poll_recv_from(
&mut self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<(usize, SocketAddr), Error>>
pub fn poll_recv_from( &mut self, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<(usize, SocketAddr), Error>>
Receives data from the socket. On success, returns the number of bytes read and the address from whence the data came.
Sourcepub fn recv_from<'a>(&'a mut self, buf: &'a mut [u8]) -> UdpRecvFrom<'a> ⓘ
pub fn recv_from<'a>(&'a mut self, buf: &'a mut [u8]) -> UdpRecvFrom<'a> ⓘ
Receives data from the socket. On success, completes with the number of bytes read and the address from whence the data came.
Sourcepub fn poll_send_to(
&mut self,
cx: &mut Context<'_>,
buf: &[u8],
target: &SocketAddr,
) -> Poll<Result<usize, Error>>
pub fn poll_send_to( &mut self, cx: &mut Context<'_>, buf: &[u8], target: &SocketAddr, ) -> Poll<Result<usize, Error>>
Sends data on the socket to the given address. On success, returns the number of bytes written.
Sourcepub fn send_to<'a>(
&'a mut self,
buf: &'a [u8],
target: &'a SocketAddr,
) -> UdpSendTo<'a> ⓘ
pub fn send_to<'a>( &'a mut self, buf: &'a [u8], target: &'a SocketAddr, ) -> UdpSendTo<'a> ⓘ
Sends data on the socket to the given address. On success, completes with the number of bytes written.
Sourcepub fn set_broadcast(&self, on: bool) -> Result<(), Error>
pub fn set_broadcast(&self, on: bool) -> Result<(), Error>
Sets the value of the SO_BROADCAST option for this socket.
When enabled, this socket is allowed to send packets to a broadcast address.
Sourcepub fn broadcast(&self) -> Result<bool, Error>
pub fn broadcast(&self) -> Result<bool, Error>
Gets the value of the SO_BROADCAST option for this socket.
For more information about this option, see set_broadcast.
Sourcepub fn set_multicast_loop_v4(&self, on: bool) -> Result<(), Error>
pub fn set_multicast_loop_v4(&self, on: bool) -> Result<(), Error>
Sets the value of the IP_MULTICAST_LOOP option for this socket.
If enabled, multicast packets will be looped back to the local socket. Note that this may not have any affect on IPv6 sockets.
Sourcepub fn multicast_loop_v4(&self) -> Result<bool, Error>
pub fn multicast_loop_v4(&self) -> Result<bool, Error>
Gets the value of the IP_MULTICAST_LOOP option for this socket.
For more information about this option, see
set_multicast_loop_v4.
Sourcepub fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<(), Error>
pub fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<(), Error>
Sets the value of the IP_MULTICAST_TTL option for this socket.
Indicates the time-to-live value of outgoing multicast packets for this socket. The default value is 1 which means that multicast packets don’t leave the local network unless explicitly requested.
Note that this may not have any affect on IPv6 sockets.
Sourcepub fn multicast_ttl_v4(&self) -> Result<u32, Error>
pub fn multicast_ttl_v4(&self) -> Result<u32, Error>
Gets the value of the IP_MULTICAST_TTL option for this socket.
For more information about this option, see
set_multicast_ttl_v4.
Sourcepub fn set_multicast_loop_v6(&self, on: bool) -> Result<(), Error>
pub fn set_multicast_loop_v6(&self, on: bool) -> Result<(), Error>
Sets the value of the IPV6_MULTICAST_LOOP option for this socket.
Controls whether this socket sees the multicast packets it sends itself. Note that this may not have any affect on IPv4 sockets.
Sourcepub fn multicast_loop_v6(&self) -> Result<bool, Error>
pub fn multicast_loop_v6(&self) -> Result<bool, Error>
Gets the value of the IPV6_MULTICAST_LOOP option for this socket.
For more information about this option, see
set_multicast_loop_v6.
Sourcepub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
pub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
Sets the value for the IP_TTL option on this socket.
This value sets the time-to-live field that is used in every packet sent from this socket.
Sourcepub fn ttl(&self) -> Result<u32, Error>
pub fn ttl(&self) -> Result<u32, Error>
Gets the value of the IP_TTL option for this socket.
For more information about this option, see set_ttl.
Sourcepub fn join_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<(), Error>
pub fn join_multicast_v4( &self, multiaddr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<(), Error>
Executes an operation of the IP_ADD_MEMBERSHIP type.
This function specifies a new multicast group for this socket to join.
The address must be a valid multicast address, and interface is the
address of the local interface with which the system should join the
multicast group. If it’s equal to INADDR_ANY then an appropriate
interface is chosen by the system.
Sourcepub fn join_multicast_v6(
&self,
multiaddr: Ipv6Addr,
interface: u32,
) -> Result<(), Error>
pub fn join_multicast_v6( &self, multiaddr: Ipv6Addr, interface: u32, ) -> Result<(), Error>
Executes an operation of the IPV6_ADD_MEMBERSHIP type.
This function specifies a new multicast group for this socket to join.
The address must be a valid multicast address, and interface is the
index of the interface to join/leave (or 0 to indicate any interface).
Sourcepub fn leave_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<(), Error>
pub fn leave_multicast_v4( &self, multiaddr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<(), Error>
Executes an operation of the IP_DROP_MEMBERSHIP type.
For more information about this option, see
join_multicast_v4.
Sourcepub fn leave_multicast_v6(
&self,
multiaddr: Ipv6Addr,
interface: u32,
) -> Result<(), Error>
pub fn leave_multicast_v6( &self, multiaddr: Ipv6Addr, interface: u32, ) -> Result<(), Error>
Executes an operation of the IPV6_DROP_MEMBERSHIP type.
For more information about this option, see
join_multicast_v6.