@@ -498,18 +498,46 @@ impl TcpStream {
498498
499499 /// Moves this TCP stream into or out of nonblocking mode.
500500 ///
501- /// On Unix this corresponds to calling fcntl, and on Windows this
502- /// corresponds to calling ioctlsocket.
501+ /// This will result in `read`, `write`, `recv` and `send` operations
502+ /// becoming nonblocking, i.e. immediately returning from their calls.
503+ /// If the IO operation is successful, `Ok` is returned and no further
504+ /// action is required. If the IO operation could not be completed and needs
505+ /// to be retried, an error with kind [`io::ErrorKind::WouldBlock`] is
506+ /// returned.
507+ ///
508+ /// On Unix platforms, calling this method corresponds to calling `fcntl`
509+ /// `FIONBIO`. On Windows calling this method corresponds to calling
510+ /// `ioctlsocket` `FIONBIO`.
503511 ///
504512 /// # Examples
505513 ///
514+ /// Reading bytes from a TCP stream in non-blocking mode:
515+ ///
506516 /// ```no_run
517+ /// use std::io::{self, Read};
507518 /// use std::net::TcpStream;
508519 ///
509- /// let stream = TcpStream::connect("127.0.0.1:8080 ")
510- /// .expect("Couldn't connect to the server...");
520+ /// let mut stream = TcpStream::connect("127.0.0.1:7878 ")
521+ /// .expect("Couldn't connect to the server...");
511522 /// stream.set_nonblocking(true).expect("set_nonblocking call failed");
523+ ///
524+ /// # fn wait_for_fd() { unimplemented!() }
525+ /// let mut buf = vec![];
526+ /// loop {
527+ /// match stream.read_to_end(&mut buf) {
528+ /// Ok(_) => break,
529+ /// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
530+ /// // wait until network socket is ready, typically implemented
531+ /// // via platform-specific APIs such as epoll or IOCP
532+ /// wait_for_fd();
533+ /// }
534+ /// Err(e) => panic!("encountered IO error: {}", e),
535+ /// };
536+ /// };
537+ /// println!("bytes: {:?}", buf);
512538 /// ```
539+ ///
540+ /// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
513541 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
514542 pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
515543 self . 0 . set_nonblocking ( nonblocking)
@@ -780,17 +808,48 @@ impl TcpListener {
780808
781809 /// Moves this TCP stream into or out of nonblocking mode.
782810 ///
783- /// On Unix this corresponds to calling fcntl, and on Windows this
784- /// corresponds to calling ioctlsocket.
811+ /// This will result in the `accept` operation becoming nonblocking,
812+ /// i.e. immediately returning from their calls. If the IO operation is
813+ /// successful, `Ok` is returned and no further action is required. If the
814+ /// IO operation could not be completed and needs to be retried, an error
815+ /// with kind [`io::ErrorKind::WouldBlock`] is returned.
816+ ///
817+ /// On Unix platforms, calling this method corresponds to calling `fcntl`
818+ /// `FIONBIO`. On Windows calling this method corresponds to calling
819+ /// `ioctlsocket` `FIONBIO`.
785820 ///
786821 /// # Examples
787822 ///
823+ /// Bind a TCP listener to an address, listen for connections, and read
824+ /// bytes in nonblocking mode:
825+ ///
788826 /// ```no_run
827+ /// use std::io;
789828 /// use std::net::TcpListener;
790829 ///
791- /// let listener = TcpListener::bind("127.0.0.1:80 ").unwrap();
830+ /// let listener = TcpListener::bind("127.0.0.1:7878 ").unwrap();
792831 /// listener.set_nonblocking(true).expect("Cannot set non-blocking");
832+ ///
833+ /// # fn wait_for_fd() { unimplemented!() }
834+ /// # fn handle_connection(stream: std::net::TcpStream) { unimplemented!() }
835+ /// for stream in listener.incoming() {
836+ /// match stream {
837+ /// Ok(s) => {
838+ /// // do something with the TcpStream
839+ /// handle_connection(s);
840+ /// }
841+ /// Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
842+ /// // wait until network socket is ready, typically implemented
843+ /// // via platform-specific APIs such as epoll or IOCP
844+ /// wait_for_fd();
845+ /// continue;
846+ /// }
847+ /// Err(e) => panic!("encountered IO error: {}", e),
848+ /// }
849+ /// }
793850 /// ```
851+ ///
852+ /// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
794853 #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
795854 pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
796855 self . 0 . set_nonblocking ( nonblocking)
0 commit comments