Skip to content

Commit

Permalink
Add UdpSocket::peer_addr
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosb1 committed Jan 8, 2021
1 parent 5c577ef commit 5fc104d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ impl UdpSocket {
self.inner.local_addr()
}

/// Returns the socket address of the remote peer this socket was connected to.
///
/// # Examples
///
#[cfg_attr(feature = "os-poll", doc = "```")]
#[cfg_attr(not(feature = "os-poll"), doc = "```ignore")]
/// # use std::error::Error;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use mio::net::UdpSocket;
///
/// let addr = "127.0.0.1:0".parse()?;
/// let peer_addr = "127.0.0.1:11100".parse()?;
/// let socket = UdpSocket::bind(addr)?;
/// socket.connect(peer_addr)?;
/// assert_eq!(socket.peer_addr()?.ip(), peer_addr.ip());
/// # Ok(())
/// # }
/// ```
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.inner.peer_addr()
}

/// Sends data on the socket to the given address. On success, returns the
/// number of bytes written.
///
Expand Down

0 comments on commit 5fc104d

Please sign in to comment.