@@ -707,10 +707,52 @@ impl fmt::Debug for TcpStream {
707
707
}
708
708
709
709
impl TcpListener {
710
+ /// Default "backlog" for [`TcpListener::bind`]. See
711
+ /// [`TcpListener::bind_with_backlog`] for an explanation of backlog
712
+ /// values.
713
+ #[ unstable( feature = "bind_with_backlog" , issue = "none" ) ]
714
+ pub const DEFAULT_BACKLOG : usize = 128 ;
715
+
710
716
/// Creates a new `TcpListener` which will be bound to the specified
711
717
/// address.
712
718
///
713
- /// The returned listener is ready for accepting connections.
719
+ /// The given backlog specifies the maximum number of outstanding
720
+ /// connections that will be buffered in the OS waiting to be accepted by
721
+ /// [`TcpListener::accept`]. The backlog argument overrides the default
722
+ /// specified by [`TcpListener::DEFAULT_BACKLOG`]; that default is
723
+ /// reasonable for most use cases.
724
+ ///
725
+ /// This function is otherwise [`TcpListener::bind`]: see that
726
+ /// documentation for full details of operation.
727
+ ///
728
+ /// # Examples
729
+ ///
730
+ /// Creates a TCP listener bound to `127.0.0.1:80` with a backlog of 1000:
731
+ ///
732
+ /// ```no_run
733
+ /// use std::net::TcpListener;
734
+ ///
735
+ /// let listener = TcpListener::bind_with_backlog("127.0.0.1:80", 1000).unwrap();
736
+ /// ```
737
+ ///
738
+ /// # Errors
739
+ ///
740
+ /// The specified backlog may be larger than supported by the underlying
741
+ /// system. In this case an [`io::Error`] with
742
+ /// [`io::ErrorKind::InvalidData`] will be returned.
743
+ #[ unstable( feature = "bind_with_backlog" , issue = "none" ) ]
744
+ pub fn bind_with_backlog < A : ToSocketAddrs > ( addr : A , backlog : usize ) -> io:: Result < TcpListener > {
745
+ super :: each_addr ( addr, move |a| net_imp:: TcpListener :: bind_with_backlog ( a, backlog) )
746
+ . map ( TcpListener )
747
+ }
748
+
749
+ /// Creates a new `TcpListener` which will be bound to the specified
750
+ /// address. The returned listener is ready for accepting
751
+ /// connections.
752
+ ///
753
+ /// The listener will have a backlog given by
754
+ /// [`TcpListener::DEFAULT_BACKLOG`]. See the documentation for
755
+ /// [`TcpListener::bind_with_backlog`] for further information.
714
756
///
715
757
/// Binding with a port number of 0 will request that the OS assigns a port
716
758
/// to this listener. The port allocated can be queried via the
@@ -748,7 +790,7 @@ impl TcpListener {
748
790
/// ```
749
791
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
750
792
pub fn bind < A : ToSocketAddrs > ( addr : A ) -> io:: Result < TcpListener > {
751
- super :: each_addr ( addr, net_imp :: TcpListener :: bind ) . map ( TcpListener )
793
+ Self :: bind_with_backlog ( addr, TcpListener :: DEFAULT_BACKLOG )
752
794
}
753
795
754
796
/// Returns the local socket address of this listener.
0 commit comments