pub struct Socket {
socket: UdpSocket,
port: u16,
}
Expand description
This struct represents a low level network socket. The socket is based on UDP and thus provides unreliable and unordered means of data delivery.
Fields§
§socket: UdpSocket
§port: u16
Implementations§
source§impl Socket
impl Socket
sourcepub async fn bind(port: Option<u16>) -> Result<Self>
pub async fn bind(port: Option<u16>) -> Result<Self>
Creates / binds a new IPv4 based connection (socket).
§Arguments
port
- if None, system assigned port is used.
pub fn port(&self) -> u16
sourcepub async fn recv(
&self,
buf: &mut [u8]
) -> Result<(usize, SocketAddr), RecvError>
pub async fn recv( &self, buf: &mut [u8] ) -> Result<(usize, SocketAddr), RecvError>
Receive a single datagram.
The returned data are guaranteed to be at most MAX_DATAGRAM_SIZE
bytes long.
§Panics
Panics if len of buf
is smaller than MAX_DATAGRAM_SIZE
.
sourcepub async fn send(
&self,
target: SocketAddr,
data: &[u8]
) -> Result<(), SendError>
pub async fn send( &self, target: SocketAddr, data: &[u8] ) -> Result<(), SendError>
Send data to a single target.
§Panics
This method panics if data
have more than MAX_DATAGRAM_SIZE
bytes.
Auto Trait Implementations§
impl Freeze for Socket
impl RefUnwindSafe for Socket
impl Send for Socket
impl Sync for Socket
impl Unpin for Socket
impl UnwindSafe for Socket
Blanket Implementations§
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