@@ -297,12 +297,30 @@ impl TcpListener {
297
297
///
298
298
/// The address type can be any implementor of `ToSocketAddrs` trait. See
299
299
/// its documentation for concrete examples.
300
+ ///
301
+ /// # Examples
302
+ ///
303
+ /// ```no_run
304
+ /// use std::net::TcpListener;
305
+ ///
306
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
307
+ /// ```
300
308
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
301
309
pub fn bind < A : ToSocketAddrs > ( addr : A ) -> io:: Result < TcpListener > {
302
310
super :: each_addr ( addr, net_imp:: TcpListener :: bind) . map ( TcpListener )
303
311
}
304
312
305
313
/// Returns the local socket address of this listener.
314
+ ///
315
+ /// # Examples
316
+ ///
317
+ /// ```no_run
318
+ /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
319
+ ///
320
+ /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
321
+ /// assert_eq!(listener.local_addr().unwrap(),
322
+ /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
323
+ /// ```
306
324
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
307
325
pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
308
326
self . 0 . socket_addr ( )
@@ -313,6 +331,15 @@ impl TcpListener {
313
331
/// The returned `TcpListener` is a reference to the same socket that this
314
332
/// object references. Both handles can be used to accept incoming
315
333
/// connections and options set on one listener will affect the other.
334
+ ///
335
+ /// # Examples
336
+ ///
337
+ /// ```no_run
338
+ /// use std::net::TcpListener;
339
+ ///
340
+ /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
341
+ /// let listener_clone = listener.try_clone().unwrap();
342
+ /// ```
316
343
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
317
344
pub fn try_clone ( & self ) -> io:: Result < TcpListener > {
318
345
self . 0 . duplicate ( ) . map ( TcpListener )
@@ -323,6 +350,18 @@ impl TcpListener {
323
350
/// This function will block the calling thread until a new TCP connection
324
351
/// is established. When established, the corresponding `TcpStream` and the
325
352
/// remote peer's address will be returned.
353
+ ///
354
+ /// # Examples
355
+ ///
356
+ /// ```no_run
357
+ /// use std::net::TcpListener;
358
+ ///
359
+ /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
360
+ /// match listener.accept() {
361
+ /// Ok((_socket, addr)) => println!("new client: {:?}", addr),
362
+ /// Err(e) => println!("couldn't get client: {:?}", e),
363
+ /// }
364
+ /// ```
326
365
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
327
366
pub fn accept ( & self ) -> io:: Result < ( TcpStream , SocketAddr ) > {
328
367
self . 0 . accept ( ) . map ( |( a, b) | ( TcpStream ( a) , b) )
@@ -331,8 +370,28 @@ impl TcpListener {
331
370
/// Returns an iterator over the connections being received on this
332
371
/// listener.
333
372
///
334
- /// The returned iterator will never return `None` and will also not yield
335
- /// the peer's `SocketAddr` structure.
373
+ /// The returned iterator will never return [`None`] and will also not yield
374
+ /// the peer's [`SocketAddr`] structure.
375
+ ///
376
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
377
+ /// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
378
+ ///
379
+ /// # Examples
380
+ ///
381
+ /// ```no_run
382
+ /// use std::net::TcpListener;
383
+ ///
384
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
385
+ ///
386
+ /// for stream in listener.incoming() {
387
+ /// match stream {
388
+ /// Ok(stream) => {
389
+ /// println!("new client!");
390
+ /// }
391
+ /// Err(e) => { /* connection failed */ }
392
+ /// }
393
+ /// }
394
+ /// ```
336
395
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
337
396
pub fn incoming ( & self ) -> Incoming {
338
397
Incoming { listener : self }
@@ -342,16 +401,35 @@ impl TcpListener {
342
401
///
343
402
/// This value sets the time-to-live field that is used in every packet sent
344
403
/// from this socket.
404
+ ///
405
+ /// # Examples
406
+ ///
407
+ /// ```no_run
408
+ /// use std::net::TcpListener;
409
+ ///
410
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
411
+ /// listener.set_ttl(100).expect("could not set TTL");
412
+ /// ```
345
413
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
346
414
pub fn set_ttl ( & self , ttl : u32 ) -> io:: Result < ( ) > {
347
415
self . 0 . set_ttl ( ttl)
348
416
}
349
417
350
418
/// Gets the value of the `IP_TTL` option for this socket.
351
419
///
352
- /// For more information about this option, see [`set_ttl`][link].
420
+ /// For more information about this option, see [`set_ttl() `][link].
353
421
///
354
422
/// [link]: #method.set_ttl
423
+ ///
424
+ /// # Examples
425
+ ///
426
+ /// ```no_run
427
+ /// use std::net::TcpListener;
428
+ ///
429
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
430
+ /// listener.set_ttl(100).expect("could not set TTL");
431
+ /// assert_eq!(listener.ttl().unwrap_or(0), 100);
432
+ /// ```
355
433
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
356
434
pub fn ttl ( & self ) -> io:: Result < u32 > {
357
435
self . 0 . ttl ( )
@@ -365,6 +443,15 @@ impl TcpListener {
365
443
///
366
444
/// If this is set to `false` then the socket can be used to send and
367
445
/// receive packets from an IPv4-mapped IPv6 address.
446
+ ///
447
+ /// # Examples
448
+ ///
449
+ /// ```no_run
450
+ /// use std::net::TcpListener;
451
+ ///
452
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
453
+ /// listener.set_only_v6(true).expect("Cannot set to IPv6");
454
+ /// ```
368
455
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
369
456
pub fn set_only_v6 ( & self , only_v6 : bool ) -> io:: Result < ( ) > {
370
457
self . 0 . set_only_v6 ( only_v6)
@@ -375,6 +462,16 @@ impl TcpListener {
375
462
/// For more information about this option, see [`set_only_v6`][link].
376
463
///
377
464
/// [link]: #method.set_only_v6
465
+ ///
466
+ /// # Examples
467
+ ///
468
+ /// ```no_run
469
+ /// use std::net::TcpListener;
470
+ ///
471
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
472
+ /// listener.set_only_v6(true).expect("Cannot set to IPv6");
473
+ /// assert_eq!(listener.only_v6().unwrap_or(false), true);
474
+ /// ```
378
475
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
379
476
pub fn only_v6 ( & self ) -> io:: Result < bool > {
380
477
self . 0 . only_v6 ( )
@@ -385,6 +482,15 @@ impl TcpListener {
385
482
/// This will retrieve the stored error in the underlying socket, clearing
386
483
/// the field in the process. This can be useful for checking errors between
387
484
/// calls.
485
+ ///
486
+ /// # Examples
487
+ ///
488
+ /// ```no_run
489
+ /// use std::net::TcpListener;
490
+ ///
491
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
492
+ /// listener.take_error().expect("No error was expected");
493
+ /// ```
388
494
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
389
495
pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
390
496
self . 0 . take_error ( )
@@ -394,6 +500,15 @@ impl TcpListener {
394
500
///
395
501
/// On Unix this corresponds to calling fcntl, and on Windows this
396
502
/// corresponds to calling ioctlsocket.
503
+ ///
504
+ /// # Examples
505
+ ///
506
+ /// ```no_run
507
+ /// use std::net::TcpListener;
508
+ ///
509
+ /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
510
+ /// listener.set_nonblocking(true).expect("Cannot set non-blocking");
511
+ /// ```
397
512
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
398
513
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
399
514
self . 0 . set_nonblocking ( nonblocking)
0 commit comments