@@ -498,18 +498,46 @@ impl TcpStream {
498
498
499
499
/// Moves this TCP stream into or out of nonblocking mode.
500
500
///
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`.
503
511
///
504
512
/// # Examples
505
513
///
514
+ /// Reading bytes from a TCP stream in non-blocking mode:
515
+ ///
506
516
/// ```no_run
517
+ /// use std::io::{self, Read};
507
518
/// use std::net::TcpStream;
508
519
///
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...");
511
522
/// 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);
512
538
/// ```
539
+ ///
540
+ /// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
513
541
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
514
542
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
515
543
self . 0 . set_nonblocking ( nonblocking)
@@ -780,17 +808,48 @@ impl TcpListener {
780
808
781
809
/// Moves this TCP stream into or out of nonblocking mode.
782
810
///
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`.
785
820
///
786
821
/// # Examples
787
822
///
823
+ /// Bind a TCP listener to an address, listen for connections, and read
824
+ /// bytes in nonblocking mode:
825
+ ///
788
826
/// ```no_run
827
+ /// use std::io;
789
828
/// use std::net::TcpListener;
790
829
///
791
- /// let listener = TcpListener::bind("127.0.0.1:80 ").unwrap();
830
+ /// let listener = TcpListener::bind("127.0.0.1:7878 ").unwrap();
792
831
/// 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
+ /// }
793
850
/// ```
851
+ ///
852
+ /// [`io::ErrorKind::WouldBlock`]: ../io/enum.ErrorKind.html#variant.WouldBlock
794
853
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
795
854
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
796
855
self . 0 . set_nonblocking ( nonblocking)
0 commit comments