Skip to content

Commit f216f1f

Browse files
Add examples for TcpListener struct
1 parent 29abe6f commit f216f1f

File tree

1 file changed

+118
-3
lines changed

1 file changed

+118
-3
lines changed

src/libstd/net/tcp.rs

+118-3
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,30 @@ impl TcpListener {
297297
///
298298
/// The address type can be any implementor of `ToSocketAddrs` trait. See
299299
/// 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+
/// ```
300308
#[stable(feature = "rust1", since = "1.0.0")]
301309
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
302310
super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener)
303311
}
304312

305313
/// 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+
/// ```
306324
#[stable(feature = "rust1", since = "1.0.0")]
307325
pub fn local_addr(&self) -> io::Result<SocketAddr> {
308326
self.0.socket_addr()
@@ -313,6 +331,15 @@ impl TcpListener {
313331
/// The returned `TcpListener` is a reference to the same socket that this
314332
/// object references. Both handles can be used to accept incoming
315333
/// 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+
/// ```
316343
#[stable(feature = "rust1", since = "1.0.0")]
317344
pub fn try_clone(&self) -> io::Result<TcpListener> {
318345
self.0.duplicate().map(TcpListener)
@@ -323,6 +350,18 @@ impl TcpListener {
323350
/// This function will block the calling thread until a new TCP connection
324351
/// is established. When established, the corresponding `TcpStream` and the
325352
/// 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+
/// ```
326365
#[stable(feature = "rust1", since = "1.0.0")]
327366
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
328367
self.0.accept().map(|(a, b)| (TcpStream(a), b))
@@ -331,8 +370,28 @@ impl TcpListener {
331370
/// Returns an iterator over the connections being received on this
332371
/// listener.
333372
///
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+
/// ```
336395
#[stable(feature = "rust1", since = "1.0.0")]
337396
pub fn incoming(&self) -> Incoming {
338397
Incoming { listener: self }
@@ -342,16 +401,35 @@ impl TcpListener {
342401
///
343402
/// This value sets the time-to-live field that is used in every packet sent
344403
/// 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+
/// ```
345413
#[stable(feature = "net2_mutators", since = "1.9.0")]
346414
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
347415
self.0.set_ttl(ttl)
348416
}
349417

350418
/// Gets the value of the `IP_TTL` option for this socket.
351419
///
352-
/// For more information about this option, see [`set_ttl`][link].
420+
/// For more information about this option, see [`set_ttl()`][link].
353421
///
354422
/// [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+
/// ```
355433
#[stable(feature = "net2_mutators", since = "1.9.0")]
356434
pub fn ttl(&self) -> io::Result<u32> {
357435
self.0.ttl()
@@ -365,6 +443,15 @@ impl TcpListener {
365443
///
366444
/// If this is set to `false` then the socket can be used to send and
367445
/// 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+
/// ```
368455
#[stable(feature = "net2_mutators", since = "1.9.0")]
369456
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
370457
self.0.set_only_v6(only_v6)
@@ -375,6 +462,16 @@ impl TcpListener {
375462
/// For more information about this option, see [`set_only_v6`][link].
376463
///
377464
/// [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+
/// ```
378475
#[stable(feature = "net2_mutators", since = "1.9.0")]
379476
pub fn only_v6(&self) -> io::Result<bool> {
380477
self.0.only_v6()
@@ -385,6 +482,15 @@ impl TcpListener {
385482
/// This will retrieve the stored error in the underlying socket, clearing
386483
/// the field in the process. This can be useful for checking errors between
387484
/// 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+
/// ```
388494
#[stable(feature = "net2_mutators", since = "1.9.0")]
389495
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
390496
self.0.take_error()
@@ -394,6 +500,15 @@ impl TcpListener {
394500
///
395501
/// On Unix this corresponds to calling fcntl, and on Windows this
396502
/// 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+
/// ```
397512
#[stable(feature = "net2_mutators", since = "1.9.0")]
398513
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
399514
self.0.set_nonblocking(nonblocking)

0 commit comments

Comments
 (0)