pub struct UdpSocket { /* private fields */ }
Expand description
A UDP socket.
After creating a UdpSocket
by [bind
]ing it to a socket address, data can be
[sent to] and [received from] any other socket address.
Although UDP is a connectionless protocol, this implementation provides an interface
to set an address where data should be sent and received from. After setting a remote
address with [connect
], data can be sent to and received from that address with
[send
] and [recv
].
Implementations§
source§impl UdpSocket
impl UdpSocket
sourcepub fn bind<A: ToSocketAddrs>(addr: A) -> Result<Self>
pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<Self>
Creates a UDP socket from the given address.
sourcepub async fn recv_from<T: IoBufMut>(
&self,
buf: T
) -> BufResult<(usize, SocketAddr), T>
pub async fn recv_from<T: IoBufMut>( &self, buf: T ) -> BufResult<(usize, SocketAddr), T>
Receives a single datagram message on the socket. On success, returns the number of bytes read and the origin.
sourcepub async fn send_to<T: IoBuf>(
&self,
buf: T,
socket_addr: SocketAddr
) -> BufResult<usize, T>
pub async fn send_to<T: IoBuf>( &self, buf: T, socket_addr: SocketAddr ) -> BufResult<usize, T>
Sends data on the socket to the given address. On success, returns the number of bytes written.
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the socket address of the remote peer this socket was connected to.
sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the socket address that this socket was created from.
sourcepub async fn connect(&self, socket_addr: SocketAddr) -> Result<()>
pub async fn connect(&self, socket_addr: SocketAddr) -> Result<()>
Connects this UDP socket to a remote address, allowing the send
and
recv
syscalls to be used to send data and also applies filters to only
receive data from the specified address.
sourcepub async fn send<T: IoBuf>(&self, buf: T) -> BufResult<usize, T>
pub async fn send<T: IoBuf>(&self, buf: T) -> BufResult<usize, T>
Sends data on the socket to the remote address to which it is connected.
sourcepub async fn recv<T: IoBufMut>(&self, buf: T) -> BufResult<usize, T>
pub async fn recv<T: IoBufMut>(&self, buf: T) -> BufResult<usize, T>
Receives a single datagram message on the socket from the remote address to which it is connected. On success, returns the number of bytes read.
sourcepub fn from_std(socket: UdpSocket) -> Result<Self>
pub fn from_std(socket: UdpSocket) -> Result<Self>
Creates new UdpSocket
from a std::net::UdpSocket
.
sourcepub fn set_reuse_address(&self, reuse: bool) -> Result<()>
pub fn set_reuse_address(&self, reuse: bool) -> Result<()>
Set value for the SO_REUSEADDR
option on this socket.
sourcepub fn set_reuse_port(&self, reuse: bool) -> Result<()>
pub fn set_reuse_port(&self, reuse: bool) -> Result<()>
Set value for the SO_REUSEPORT
option on this socket.
sourcepub async fn readable(&self, relaxed: bool) -> Result<()>
pub async fn readable(&self, relaxed: bool) -> Result<()>
Wait for read readiness. Note: Do not use it before every io. It is different from other runtimes!
Everytime call to this method may pay a syscall cost. In uring impl, it will push a PollAdd op; in epoll impl, it will use use inner readiness state; if !relaxed, it will call syscall poll after that.
If relaxed, on legacy driver it may return false positive result. If you want to do io by your own, you must maintain io readiness and wait for io ready with relaxed=false.
sourcepub async fn writable(&self, relaxed: bool) -> Result<()>
pub async fn writable(&self, relaxed: bool) -> Result<()>
Wait for write readiness. Note: Do not use it before every io. It is different from other runtimes!
Everytime call to this method may pay a syscall cost. In uring impl, it will push a PollAdd op; in epoll impl, it will use use inner readiness state; if !relaxed, it will call syscall poll after that.
If relaxed, on legacy driver it may return false positive result. If you want to do io by your own, you must maintain io readiness and wait for io ready with relaxed=false.
source§impl UdpSocket
impl UdpSocket
Cancelable related methods
sourcepub async fn cancelable_recv_from<T: IoBufMut>(
&self,
buf: T,
c: CancelHandle
) -> BufResult<(usize, SocketAddr), T>
pub async fn cancelable_recv_from<T: IoBufMut>( &self, buf: T, c: CancelHandle ) -> BufResult<(usize, SocketAddr), T>
Receives a single datagram message on the socket. On success, returns the number of bytes read and the origin.
sourcepub async fn cancelable_send_to<T: IoBuf>(
&self,
buf: T,
socket_addr: SocketAddr,
c: CancelHandle
) -> BufResult<usize, T>
pub async fn cancelable_send_to<T: IoBuf>( &self, buf: T, socket_addr: SocketAddr, c: CancelHandle ) -> BufResult<usize, T>
Sends data on the socket to the given address. On success, returns the number of bytes written.
sourcepub async fn cancelable_send<T: IoBuf>(
&self,
buf: T,
c: CancelHandle
) -> BufResult<usize, T>
pub async fn cancelable_send<T: IoBuf>( &self, buf: T, c: CancelHandle ) -> BufResult<usize, T>
Sends data on the socket to the remote address to which it is connected.
sourcepub async fn cancelable_recv<T: IoBufMut>(
&self,
buf: T,
c: CancelHandle
) -> BufResult<usize, T>
pub async fn cancelable_recv<T: IoBufMut>( &self, buf: T, c: CancelHandle ) -> BufResult<usize, T>
Receives a single datagram message on the socket from the remote address to which it is connected. On success, returns the number of bytes read.
Trait Implementations§
impl Split for UdpSocket
UdpSocket is safe to split to two parts