Skip to content

Commit

Permalink
net: add peer_addr to UdpSocket (#4611)
Browse files Browse the repository at this point in the history
The std UdpSocket has the peer_addr method which can be used to get the address of the remote peer the socket is connected to.

Fixes: #4609
  • Loading branch information
PoorlyDefinedBehaviour committed Apr 10, 2022
1 parent 83477c7 commit 252b0fa
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tokio/src/net/udp.rs
Expand Up @@ -278,6 +278,28 @@ impl UdpSocket {
self.io.local_addr()
}

/// Returns the socket address of the remote peer this socket was connected to.
///
/// # Example
///
/// ```
/// use tokio::net::UdpSocket;
///
/// # use std::{io, net::SocketAddr};
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// let addr = "0.0.0.0:8080".parse::<SocketAddr>().unwrap();
/// let peer = "127.0.0.1:11100".parse::<SocketAddr>().unwrap();
/// let sock = UdpSocket::bind(addr).await?;
/// sock.connect(peer).await?;
/// assert_eq!(peer, sock.peer_addr()?);
/// # Ok(())
/// # }
/// ```
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.io.peer_addr()
}

/// Connects the UDP socket setting the default destination for send() and
/// limiting packets that are read via recv from the address specified in
/// `addr`.
Expand Down

0 comments on commit 252b0fa

Please sign in to comment.