@@ -180,6 +180,37 @@ impl UdpSocket {
180180 }
181181 }
182182
183+ /// Returns the socket address of the remote peer this socket was connected to.
184+ ///
185+ /// # Examples
186+ ///
187+ /// ```no_run
188+ /// #![feature(udp_peer_addr)]
189+ /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
190+ ///
191+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
192+ /// socket.connect("192.168.0.1:41203").expect("couldn't connect to address");
193+ /// assert_eq!(socket.peer_addr().unwrap(),
194+ /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203)));
195+ /// ```
196+ ///
197+ /// If the socket isn't connected, it will return a [`NotConnected`] error.
198+ ///
199+ /// [`NotConnected`]: ../../std/io/enum.ErrorKind.html#variant.NotConnected
200+ ///
201+ /// ```no_run
202+ /// #![feature(udp_peer_addr)]
203+ /// use std::net::UdpSocket;
204+ ///
205+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
206+ /// assert_eq!(socket.peer_addr().unwrap_err().kind(),
207+ /// ::std::io::ErrorKind::NotConnected);
208+ /// ```
209+ #[ unstable( feature = "udp_peer_addr" , issue = "59127" ) ]
210+ pub fn peer_addr ( & self ) -> io:: Result < SocketAddr > {
211+ self . 0 . peer_addr ( )
212+ }
213+
183214 /// Returns the socket address that this socket was created from.
184215 ///
185216 /// # Examples
@@ -865,13 +896,23 @@ mod tests {
865896 }
866897
867898 #[ test]
868- fn socket_name_ip4 ( ) {
899+ fn socket_name ( ) {
869900 each_ip ( & mut |addr, _| {
870901 let server = t ! ( UdpSocket :: bind( & addr) ) ;
871902 assert_eq ! ( addr, t!( server. local_addr( ) ) ) ;
872903 } )
873904 }
874905
906+ #[ test]
907+ fn socket_peer ( ) {
908+ each_ip ( & mut |addr1, addr2| {
909+ let server = t ! ( UdpSocket :: bind( & addr1) ) ;
910+ assert_eq ! ( server. peer_addr( ) . unwrap_err( ) . kind( ) , ErrorKind :: NotConnected ) ;
911+ t ! ( server. connect( & addr2) ) ;
912+ assert_eq ! ( addr2, t!( server. peer_addr( ) ) ) ;
913+ } )
914+ }
915+
875916 #[ test]
876917 fn udp_clone_smoke ( ) {
877918 each_ip ( & mut |addr1, addr2| {
0 commit comments