Skip to content

Commit 5fc104d

Browse files
authored
Add UdpSocket::peer_addr
1 parent 5c577ef commit 5fc104d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/net/udp.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ impl UdpSocket {
161161
self.inner.local_addr()
162162
}
163163

164+
/// Returns the socket address of the remote peer this socket was connected to.
165+
///
166+
/// # Examples
167+
///
168+
#[cfg_attr(feature = "os-poll", doc = "```")]
169+
#[cfg_attr(not(feature = "os-poll"), doc = "```ignore")]
170+
/// # use std::error::Error;
171+
/// #
172+
/// # fn main() -> Result<(), Box<dyn Error>> {
173+
/// use mio::net::UdpSocket;
174+
///
175+
/// let addr = "127.0.0.1:0".parse()?;
176+
/// let peer_addr = "127.0.0.1:11100".parse()?;
177+
/// let socket = UdpSocket::bind(addr)?;
178+
/// socket.connect(peer_addr)?;
179+
/// assert_eq!(socket.peer_addr()?.ip(), peer_addr.ip());
180+
/// # Ok(())
181+
/// # }
182+
/// ```
183+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
184+
self.inner.peer_addr()
185+
}
186+
164187
/// Sends data on the socket to the given address. On success, returns the
165188
/// number of bytes written.
166189
///

0 commit comments

Comments
 (0)