1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use net::{ToSocketAddrs, SocketAddr, Shutdown};
16 use sys_common::net as net_imp;
17 use sys_common::{AsInner, FromInner, IntoInner};
20 /// A structure which represents a TCP stream between a local socket and a
23 /// The socket will be closed when the value is dropped.
28 /// use std::io::prelude::*;
29 /// use std::net::TcpStream;
32 /// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
34 /// // ignore the Result
35 /// let _ = stream.write(&[1]);
36 /// let _ = stream.read(&mut [0; 128]); // ignore here too
37 /// } // the stream is closed here
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub struct TcpStream(net_imp::TcpStream);
42 /// A structure representing a socket server.
47 /// use std::net::{TcpListener, TcpStream};
49 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
51 /// fn handle_client(stream: TcpStream) {
55 /// // accept connections and process them serially
56 /// for stream in listener.incoming() {
59 /// handle_client(stream);
61 /// Err(e) => { /* connection failed */ }
65 #[stable(feature = "rust1", since = "1.0.0")]
66 pub struct TcpListener(net_imp::TcpListener);
68 /// An infinite iterator over the connections from a `TcpListener`.
70 /// This iterator will infinitely yield [`Some`] of the accepted connections. It
71 /// is equivalent to calling `accept` in a loop.
73 /// This `struct` is created by the [`incoming`] method on [`TcpListener`].
75 /// [`Some`]: ../../std/option/enum.Option.html#variant.Some
76 /// [`incoming`]: struct.TcpListener.html#method.incoming
77 /// [`TcpListener`]: struct.TcpListener.html
78 #[stable(feature = "rust1", since = "1.0.0")]
80 pub struct Incoming<'a> { listener: &'a TcpListener }
83 /// Opens a TCP connection to a remote host.
85 /// `addr` is an address of the remote host. Anything which implements
86 /// `ToSocketAddrs` trait can be supplied for the address; see this trait
87 /// documentation for concrete examples.
88 /// In case `ToSocketAddrs::to_socket_addrs()` returns more than one entry,
89 /// then the first valid and reachable address is used.
94 /// use std::net::TcpStream;
96 /// if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") {
97 /// println!("Connected to the server!");
99 /// println!("Couldn't connect to server...");
102 #[stable(feature = "rust1", since = "1.0.0")]
103 pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> {
104 super::each_addr(addr, net_imp::TcpStream::connect).map(TcpStream)
107 /// Returns the socket address of the remote peer of this TCP connection.
112 /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
114 /// let stream = TcpStream::connect("127.0.0.1:8080")
115 /// .expect("Couldn't connect to the server...");
116 /// assert_eq!(stream.peer_addr().unwrap(),
117 /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
119 #[stable(feature = "rust1", since = "1.0.0")]
120 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
124 /// Returns the socket address of the local half of this TCP connection.
129 /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
131 /// let stream = TcpStream::connect("127.0.0.1:8080")
132 /// .expect("Couldn't connect to the server...");
133 /// assert_eq!(stream.local_addr().unwrap(),
134 /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
136 #[stable(feature = "rust1", since = "1.0.0")]
137 pub fn local_addr(&self) -> io::Result<SocketAddr> {
141 /// Shuts down the read, write, or both halves of this connection.
143 /// This function will cause all pending and future I/O on the specified
144 /// portions to return immediately with an appropriate value (see the
145 /// documentation of [`Shutdown`]).
147 /// [`Shutdown`]: ../../std/net/enum.Shutdown.html
152 /// use std::net::{Shutdown, TcpStream};
154 /// let stream = TcpStream::connect("127.0.0.1:8080")
155 /// .expect("Couldn't connect to the server...");
156 /// stream.shutdown(Shutdown::Both).expect("shutdown call failed");
158 #[stable(feature = "rust1", since = "1.0.0")]
159 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
163 /// Creates a new independently owned handle to the underlying socket.
165 /// The returned `TcpStream` is a reference to the same stream that this
166 /// object references. Both handles will read and write the same stream of
167 /// data, and options set on one stream will be propagated to the other
173 /// use std::net::TcpStream;
175 /// let stream = TcpStream::connect("127.0.0.1:8080")
176 /// .expect("Couldn't connect to the server...");
177 /// let stream_clone = stream.try_clone().expect("clone failed...");
179 #[stable(feature = "rust1", since = "1.0.0")]
180 pub fn try_clone(&self) -> io::Result<TcpStream> {
181 self.0.duplicate().map(TcpStream)
184 /// Sets the read timeout to the timeout specified.
186 /// If the value specified is [`None`], then [`read`] calls will block
187 /// indefinitely. It is an error to pass the zero `Duration` to this
192 /// Platforms may return a different error code whenever a read times out as
193 /// a result of setting this option. For example Unix typically returns an
194 /// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
196 /// [`None`]: ../../std/option/enum.Option.html#variant.None
197 /// [`read`]: ../../std/io/trait.Read.html#tymethod.read
198 /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
199 /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
204 /// use std::net::TcpStream;
206 /// let stream = TcpStream::connect("127.0.0.1:8080")
207 /// .expect("Couldn't connect to the server...");
208 /// stream.set_read_timeout(None).expect("set_read_timeout call failed");
210 #[stable(feature = "socket_timeout", since = "1.4.0")]
211 pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
212 self.0.set_read_timeout(dur)
215 /// Sets the write timeout to the timeout specified.
217 /// If the value specified is [`None`], then [`write`] calls will block
218 /// indefinitely. It is an error to pass the zero [`Duration`] to this
223 /// Platforms may return a different error code whenever a write times out
224 /// as a result of setting this option. For example Unix typically returns
225 /// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
227 /// [`None`]: ../../std/option/enum.Option.html#variant.None
228 /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
229 /// [`Duration`]: ../../std/time/struct.Duration.html
230 /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
231 /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
236 /// use std::net::TcpStream;
238 /// let stream = TcpStream::connect("127.0.0.1:8080")
239 /// .expect("Couldn't connect to the server...");
240 /// stream.set_write_timeout(None).expect("set_write_timeout call failed");
242 #[stable(feature = "socket_timeout", since = "1.4.0")]
243 pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
244 self.0.set_write_timeout(dur)
247 /// Returns the read timeout of this socket.
249 /// If the timeout is [`None`], then [`read`] calls will block indefinitely.
253 /// Some platforms do not provide access to the current timeout.
255 /// [`None`]: ../../std/option/enum.Option.html#variant.None
256 /// [`read`]: ../../std/io/trait.Read.html#tymethod.read
261 /// use std::net::TcpStream;
263 /// let stream = TcpStream::connect("127.0.0.1:8080")
264 /// .expect("Couldn't connect to the server...");
265 /// stream.set_read_timeout(None).expect("set_read_timeout call failed");
266 /// assert_eq!(stream.read_timeout().unwrap(), None);
268 #[stable(feature = "socket_timeout", since = "1.4.0")]
269 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
270 self.0.read_timeout()
273 /// Returns the write timeout of this socket.
275 /// If the timeout is [`None`], then [`write`] calls will block indefinitely.
279 /// Some platforms do not provide access to the current timeout.
281 /// [`None`]: ../../std/option/enum.Option.html#variant.None
282 /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
287 /// use std::net::TcpStream;
289 /// let stream = TcpStream::connect("127.0.0.1:8080")
290 /// .expect("Couldn't connect to the server...");
291 /// stream.set_write_timeout(None).expect("set_write_timeout call failed");
292 /// assert_eq!(stream.write_timeout().unwrap(), None);
294 #[stable(feature = "socket_timeout", since = "1.4.0")]
295 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
296 self.0.write_timeout()
299 /// Receives data on the socket from the remote adress to which it is
300 /// connected, without removing that data from the queue. On success,
301 /// returns the number of bytes peeked.
303 /// Successive calls return the same data. This is accomplished by passing
304 /// `MSG_PEEK` as a flag to the underlying `recv` system call.
309 /// #![feature(peek)]
310 /// use std::net::TcpStream;
312 /// let stream = TcpStream::connect("127.0.0.1:8000")
313 /// .expect("couldn't bind to address");
314 /// let mut buf = [0; 10];
315 /// let len = stream.peek(&mut buf).expect("peek failed");
317 #[unstable(feature = "peek", issue = "38980")]
318 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
322 /// Sets the value of the `TCP_NODELAY` option on this socket.
324 /// If set, this option disables the Nagle algorithm. This means that
325 /// segments are always sent as soon as possible, even if there is only a
326 /// small amount of data. When not set, data is buffered until there is a
327 /// sufficient amount to send out, thereby avoiding the frequent sending of
333 /// use std::net::TcpStream;
335 /// let stream = TcpStream::connect("127.0.0.1:8080")
336 /// .expect("Couldn't connect to the server...");
337 /// stream.set_nodelay(true).expect("set_nodelay call failed");
339 #[stable(feature = "net2_mutators", since = "1.9.0")]
340 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
341 self.0.set_nodelay(nodelay)
344 /// Gets the value of the `TCP_NODELAY` option on this socket.
346 /// For more information about this option, see [`set_nodelay`][link].
348 /// [link]: #method.set_nodelay
353 /// use std::net::TcpStream;
355 /// let stream = TcpStream::connect("127.0.0.1:8080")
356 /// .expect("Couldn't connect to the server...");
357 /// stream.set_nodelay(true).expect("set_nodelay call failed");
358 /// assert_eq!(stream.nodelay().unwrap_or(false), true);
360 #[stable(feature = "net2_mutators", since = "1.9.0")]
361 pub fn nodelay(&self) -> io::Result<bool> {
365 /// Sets the value for the `IP_TTL` option on this socket.
367 /// This value sets the time-to-live field that is used in every packet sent
368 /// from this socket.
373 /// use std::net::TcpStream;
375 /// let stream = TcpStream::connect("127.0.0.1:8080")
376 /// .expect("Couldn't connect to the server...");
377 /// stream.set_ttl(100).expect("set_ttl call failed");
379 #[stable(feature = "net2_mutators", since = "1.9.0")]
380 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
384 /// Gets the value of the `IP_TTL` option for this socket.
386 /// For more information about this option, see [`set_ttl`][link].
388 /// [link]: #method.set_ttl
393 /// use std::net::TcpStream;
395 /// let stream = TcpStream::connect("127.0.0.1:8080")
396 /// .expect("Couldn't connect to the server...");
397 /// stream.set_ttl(100).expect("set_ttl call failed");
398 /// assert_eq!(stream.ttl().unwrap_or(0), 100);
400 #[stable(feature = "net2_mutators", since = "1.9.0")]
401 pub fn ttl(&self) -> io::Result<u32> {
405 /// Get the value of the `SO_ERROR` option on this socket.
407 /// This will retrieve the stored error in the underlying socket, clearing
408 /// the field in the process. This can be useful for checking errors between
414 /// use std::net::TcpStream;
416 /// let stream = TcpStream::connect("127.0.0.1:8080")
417 /// .expect("Couldn't connect to the server...");
418 /// stream.take_error().expect("No error was expected...");
420 #[stable(feature = "net2_mutators", since = "1.9.0")]
421 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
425 /// Moves this TCP stream into or out of nonblocking mode.
427 /// On Unix this corresponds to calling fcntl, and on Windows this
428 /// corresponds to calling ioctlsocket.
433 /// use std::net::TcpStream;
435 /// let stream = TcpStream::connect("127.0.0.1:8080")
436 /// .expect("Couldn't connect to the server...");
437 /// stream.set_nonblocking(true).expect("set_nonblocking call failed");
439 #[stable(feature = "net2_mutators", since = "1.9.0")]
440 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
441 self.0.set_nonblocking(nonblocking)
445 #[stable(feature = "rust1", since = "1.0.0")]
446 impl Read for TcpStream {
447 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) }
448 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
449 self.0.read_to_end(buf)
452 #[stable(feature = "rust1", since = "1.0.0")]
453 impl Write for TcpStream {
454 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }
455 fn flush(&mut self) -> io::Result<()> { Ok(()) }
457 #[stable(feature = "rust1", since = "1.0.0")]
458 impl<'a> Read for &'a TcpStream {
459 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) }
460 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
461 self.0.read_to_end(buf)
464 #[stable(feature = "rust1", since = "1.0.0")]
465 impl<'a> Write for &'a TcpStream {
466 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) }
467 fn flush(&mut self) -> io::Result<()> { Ok(()) }
470 impl AsInner<net_imp::TcpStream> for TcpStream {
471 fn as_inner(&self) -> &net_imp::TcpStream { &self.0 }
474 impl FromInner<net_imp::TcpStream> for TcpStream {
475 fn from_inner(inner: net_imp::TcpStream) -> TcpStream { TcpStream(inner) }
478 impl IntoInner<net_imp::TcpStream> for TcpStream {
479 fn into_inner(self) -> net_imp::TcpStream { self.0 }
482 #[stable(feature = "rust1", since = "1.0.0")]
483 impl fmt::Debug for TcpStream {
484 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
490 /// Creates a new `TcpListener` which will be bound to the specified
493 /// The returned listener is ready for accepting connections.
495 /// Binding with a port number of 0 will request that the OS assigns a port
496 /// to this listener. The port allocated can be queried via the
497 /// `local_addr` method.
499 /// The address type can be any implementor of `ToSocketAddrs` trait. See
500 /// its documentation for concrete examples.
505 /// use std::net::TcpListener;
507 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
509 #[stable(feature = "rust1", since = "1.0.0")]
510 pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
511 super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener)
514 /// Returns the local socket address of this listener.
519 /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
521 /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
522 /// assert_eq!(listener.local_addr().unwrap(),
523 /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
525 #[stable(feature = "rust1", since = "1.0.0")]
526 pub fn local_addr(&self) -> io::Result<SocketAddr> {
530 /// Creates a new independently owned handle to the underlying socket.
532 /// The returned `TcpListener` is a reference to the same socket that this
533 /// object references. Both handles can be used to accept incoming
534 /// connections and options set on one listener will affect the other.
539 /// use std::net::TcpListener;
541 /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
542 /// let listener_clone = listener.try_clone().unwrap();
544 #[stable(feature = "rust1", since = "1.0.0")]
545 pub fn try_clone(&self) -> io::Result<TcpListener> {
546 self.0.duplicate().map(TcpListener)
549 /// Accept a new incoming connection from this listener.
551 /// This function will block the calling thread until a new TCP connection
552 /// is established. When established, the corresponding `TcpStream` and the
553 /// remote peer's address will be returned.
558 /// use std::net::TcpListener;
560 /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
561 /// match listener.accept() {
562 /// Ok((_socket, addr)) => println!("new client: {:?}", addr),
563 /// Err(e) => println!("couldn't get client: {:?}", e),
566 #[stable(feature = "rust1", since = "1.0.0")]
567 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
568 self.0.accept().map(|(a, b)| (TcpStream(a), b))
571 /// Returns an iterator over the connections being received on this
574 /// The returned iterator will never return [`None`] and will also not yield
575 /// the peer's [`SocketAddr`] structure.
577 /// [`None`]: ../../std/option/enum.Option.html#variant.None
578 /// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
583 /// use std::net::TcpListener;
585 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
587 /// for stream in listener.incoming() {
590 /// println!("new client!");
592 /// Err(e) => { /* connection failed */ }
596 #[stable(feature = "rust1", since = "1.0.0")]
597 pub fn incoming(&self) -> Incoming {
598 Incoming { listener: self }
601 /// Sets the value for the `IP_TTL` option on this socket.
603 /// This value sets the time-to-live field that is used in every packet sent
604 /// from this socket.
609 /// use std::net::TcpListener;
611 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
612 /// listener.set_ttl(100).expect("could not set TTL");
614 #[stable(feature = "net2_mutators", since = "1.9.0")]
615 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
619 /// Gets the value of the `IP_TTL` option for this socket.
621 /// For more information about this option, see [`set_ttl`][link].
623 /// [link]: #method.set_ttl
628 /// use std::net::TcpListener;
630 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
631 /// listener.set_ttl(100).expect("could not set TTL");
632 /// assert_eq!(listener.ttl().unwrap_or(0), 100);
634 #[stable(feature = "net2_mutators", since = "1.9.0")]
635 pub fn ttl(&self) -> io::Result<u32> {
639 #[stable(feature = "net2_mutators", since = "1.9.0")]
640 #[rustc_deprecated(since = "1.16.0",
641 reason = "this option can only be set before the socket is bound")]
642 #[allow(missing_docs)]
643 pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
644 self.0.set_only_v6(only_v6)
647 #[stable(feature = "net2_mutators", since = "1.9.0")]
648 #[rustc_deprecated(since = "1.16.0",
649 reason = "this option can only be set before the socket is bound")]
650 #[allow(missing_docs)]
651 pub fn only_v6(&self) -> io::Result<bool> {
655 /// Get the value of the `SO_ERROR` option on this socket.
657 /// This will retrieve the stored error in the underlying socket, clearing
658 /// the field in the process. This can be useful for checking errors between
664 /// use std::net::TcpListener;
666 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
667 /// listener.take_error().expect("No error was expected");
669 #[stable(feature = "net2_mutators", since = "1.9.0")]
670 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
674 /// Moves this TCP stream into or out of nonblocking mode.
676 /// On Unix this corresponds to calling fcntl, and on Windows this
677 /// corresponds to calling ioctlsocket.
682 /// use std::net::TcpListener;
684 /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
685 /// listener.set_nonblocking(true).expect("Cannot set non-blocking");
687 #[stable(feature = "net2_mutators", since = "1.9.0")]
688 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
689 self.0.set_nonblocking(nonblocking)
693 #[stable(feature = "rust1", since = "1.0.0")]
694 impl<'a> Iterator for Incoming<'a> {
695 type Item = io::Result<TcpStream>;
696 fn next(&mut self) -> Option<io::Result<TcpStream>> {
697 Some(self.listener.accept().map(|p| p.0))
701 impl AsInner<net_imp::TcpListener> for TcpListener {
702 fn as_inner(&self) -> &net_imp::TcpListener { &self.0 }
705 impl FromInner<net_imp::TcpListener> for TcpListener {
706 fn from_inner(inner: net_imp::TcpListener) -> TcpListener {
711 impl IntoInner<net_imp::TcpListener> for TcpListener {
712 fn into_inner(self) -> net_imp::TcpListener { self.0 }
715 #[stable(feature = "rust1", since = "1.0.0")]
716 impl fmt::Debug for TcpListener {
717 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
722 #[cfg(all(test, not(target_os = "emscripten")))]
727 use net::test::{next_test_ip4, next_test_ip6};
728 use sync::mpsc::channel;
729 use sys_common::AsInner;
730 use time::{Instant, Duration};
733 fn each_ip(f: &mut FnMut(SocketAddr)) {
742 Err(e) => panic!("received error for `{}`: {}", stringify!($e), e),
749 match TcpListener::bind("1.1.1.1:9999") {
752 assert_eq!(e.kind(), ErrorKind::AddrNotAvailable),
758 match TcpStream::connect("0.0.0.0:1") {
760 Err(e) => assert!(e.kind() == ErrorKind::ConnectionRefused ||
761 e.kind() == ErrorKind::InvalidInput ||
762 e.kind() == ErrorKind::AddrInUse ||
763 e.kind() == ErrorKind::AddrNotAvailable,
764 "bad error: {} {:?}", e, e.kind()),
769 fn listen_localhost() {
770 let socket_addr = next_test_ip4();
771 let listener = t!(TcpListener::bind(&socket_addr));
773 let _t = thread::spawn(move || {
774 let mut stream = t!(TcpStream::connect(&("localhost",
775 socket_addr.port())));
776 t!(stream.write(&[144]));
779 let mut stream = t!(listener.accept()).0;
781 t!(stream.read(&mut buf));
782 assert!(buf[0] == 144);
786 fn connect_loopback() {
787 each_ip(&mut |addr| {
788 let acceptor = t!(TcpListener::bind(&addr));
790 let _t = thread::spawn(move|| {
791 let host = match addr {
792 SocketAddr::V4(..) => "127.0.0.1",
793 SocketAddr::V6(..) => "::1",
795 let mut stream = t!(TcpStream::connect(&(host, addr.port())));
796 t!(stream.write(&[66]));
799 let mut stream = t!(acceptor.accept()).0;
801 t!(stream.read(&mut buf));
802 assert!(buf[0] == 66);
808 each_ip(&mut |addr| {
809 let acceptor = t!(TcpListener::bind(&addr));
811 let (tx, rx) = channel();
812 let _t = thread::spawn(move|| {
813 let mut stream = t!(TcpStream::connect(&addr));
814 t!(stream.write(&[99]));
815 tx.send(t!(stream.local_addr())).unwrap();
818 let (mut stream, addr) = t!(acceptor.accept());
820 t!(stream.read(&mut buf));
821 assert!(buf[0] == 99);
822 assert_eq!(addr, t!(rx.recv()));
828 each_ip(&mut |addr| {
829 let acceptor = t!(TcpListener::bind(&addr));
831 let _t = thread::spawn(move|| {
832 let _stream = t!(TcpStream::connect(&addr));
836 let mut stream = t!(acceptor.accept()).0;
838 let nread = t!(stream.read(&mut buf));
839 assert_eq!(nread, 0);
840 let nread = t!(stream.read(&mut buf));
841 assert_eq!(nread, 0);
847 each_ip(&mut |addr| {
848 let acceptor = t!(TcpListener::bind(&addr));
850 let (tx, rx) = channel();
851 let _t = thread::spawn(move|| {
852 drop(t!(TcpStream::connect(&addr)));
853 tx.send(()).unwrap();
856 let mut stream = t!(acceptor.accept()).0;
859 match stream.write(&buf) {
862 assert!(e.kind() == ErrorKind::ConnectionReset ||
863 e.kind() == ErrorKind::BrokenPipe ||
864 e.kind() == ErrorKind::ConnectionAborted,
865 "unknown error: {}", e);
872 fn multiple_connect_serial() {
873 each_ip(&mut |addr| {
875 let acceptor = t!(TcpListener::bind(&addr));
877 let _t = thread::spawn(move|| {
879 let mut stream = t!(TcpStream::connect(&addr));
880 t!(stream.write(&[99]));
884 for stream in acceptor.incoming().take(max) {
885 let mut stream = t!(stream);
887 t!(stream.read(&mut buf));
888 assert_eq!(buf[0], 99);
894 fn multiple_connect_interleaved_greedy_schedule() {
895 const MAX: usize = 10;
896 each_ip(&mut |addr| {
897 let acceptor = t!(TcpListener::bind(&addr));
899 let _t = thread::spawn(move|| {
900 let acceptor = acceptor;
901 for (i, stream) in acceptor.incoming().enumerate().take(MAX) {
902 // Start another thread to handle the connection
903 let _t = thread::spawn(move|| {
904 let mut stream = t!(stream);
906 t!(stream.read(&mut buf));
907 assert!(buf[0] == i as u8);
915 fn connect(i: usize, addr: SocketAddr) {
916 if i == MAX { return }
918 let t = thread::spawn(move|| {
919 let mut stream = t!(TcpStream::connect(&addr));
920 // Connect again before writing
921 connect(i + 1, addr);
922 t!(stream.write(&[i as u8]));
924 t.join().ok().unwrap();
929 fn multiple_connect_interleaved_lazy_schedule() {
930 const MAX: usize = 10;
931 each_ip(&mut |addr| {
932 let acceptor = t!(TcpListener::bind(&addr));
934 let _t = thread::spawn(move|| {
935 for stream in acceptor.incoming().take(MAX) {
936 // Start another thread to handle the connection
937 let _t = thread::spawn(move|| {
938 let mut stream = t!(stream);
940 t!(stream.read(&mut buf));
941 assert!(buf[0] == 99);
949 fn connect(i: usize, addr: SocketAddr) {
950 if i == MAX { return }
952 let t = thread::spawn(move|| {
953 let mut stream = t!(TcpStream::connect(&addr));
954 connect(i + 1, addr);
955 t!(stream.write(&[99]));
957 t.join().ok().unwrap();
962 fn socket_and_peer_name() {
963 each_ip(&mut |addr| {
964 let listener = t!(TcpListener::bind(&addr));
965 let so_name = t!(listener.local_addr());
966 assert_eq!(addr, so_name);
967 let _t = thread::spawn(move|| {
968 t!(listener.accept());
971 let stream = t!(TcpStream::connect(&addr));
972 assert_eq!(addr, t!(stream.peer_addr()));
978 each_ip(&mut |addr| {
979 let (tx, rx) = channel();
980 let srv = t!(TcpListener::bind(&addr));
981 let _t = thread::spawn(move|| {
982 let mut cl = t!(srv.accept()).0;
983 cl.write(&[10]).unwrap();
986 tx.send(()).unwrap();
989 let mut c = t!(TcpStream::connect(&addr));
991 assert_eq!(c.read(&mut b).unwrap(), 1);
999 each_ip(&mut |addr| {
1000 let _listener = t!(TcpListener::bind(&addr));
1001 match TcpListener::bind(&addr) {
1004 assert!(e.kind() == ErrorKind::ConnectionRefused ||
1005 e.kind() == ErrorKind::Other ||
1006 e.kind() == ErrorKind::AddrInUse,
1007 "unknown error: {} {:?}", e, e.kind());
1015 each_ip(&mut |addr| {
1016 let acceptor = t!(TcpListener::bind(&addr));
1018 let _t = thread::spawn(move|| {
1019 t!(TcpStream::connect(&addr));
1022 t!(acceptor.accept());
1024 t!(TcpListener::bind(&addr));
1029 fn tcp_clone_smoke() {
1030 each_ip(&mut |addr| {
1031 let acceptor = t!(TcpListener::bind(&addr));
1033 let _t = thread::spawn(move|| {
1034 let mut s = t!(TcpStream::connect(&addr));
1035 let mut buf = [0, 0];
1036 assert_eq!(s.read(&mut buf).unwrap(), 1);
1037 assert_eq!(buf[0], 1);
1041 let mut s1 = t!(acceptor.accept()).0;
1042 let s2 = t!(s1.try_clone());
1044 let (tx1, rx1) = channel();
1045 let (tx2, rx2) = channel();
1046 let _t = thread::spawn(move|| {
1048 rx1.recv().unwrap();
1050 tx2.send(()).unwrap();
1052 tx1.send(()).unwrap();
1053 let mut buf = [0, 0];
1054 assert_eq!(s1.read(&mut buf).unwrap(), 1);
1055 rx2.recv().unwrap();
1060 fn tcp_clone_two_read() {
1061 each_ip(&mut |addr| {
1062 let acceptor = t!(TcpListener::bind(&addr));
1063 let (tx1, rx) = channel();
1064 let tx2 = tx1.clone();
1066 let _t = thread::spawn(move|| {
1067 let mut s = t!(TcpStream::connect(&addr));
1074 let mut s1 = t!(acceptor.accept()).0;
1075 let s2 = t!(s1.try_clone());
1077 let (done, rx) = channel();
1078 let _t = thread::spawn(move|| {
1080 let mut buf = [0, 0];
1081 t!(s2.read(&mut buf));
1082 tx2.send(()).unwrap();
1083 done.send(()).unwrap();
1085 let mut buf = [0, 0];
1086 t!(s1.read(&mut buf));
1087 tx1.send(()).unwrap();
1094 fn tcp_clone_two_write() {
1095 each_ip(&mut |addr| {
1096 let acceptor = t!(TcpListener::bind(&addr));
1098 let _t = thread::spawn(move|| {
1099 let mut s = t!(TcpStream::connect(&addr));
1100 let mut buf = [0, 1];
1101 t!(s.read(&mut buf));
1102 t!(s.read(&mut buf));
1105 let mut s1 = t!(acceptor.accept()).0;
1106 let s2 = t!(s1.try_clone());
1108 let (done, rx) = channel();
1109 let _t = thread::spawn(move|| {
1112 done.send(()).unwrap();
1121 fn shutdown_smoke() {
1122 each_ip(&mut |addr| {
1123 let a = t!(TcpListener::bind(&addr));
1124 let _t = thread::spawn(move|| {
1125 let mut c = t!(a.accept()).0;
1127 assert_eq!(c.read(&mut b).unwrap(), 0);
1131 let mut s = t!(TcpStream::connect(&addr));
1132 t!(s.shutdown(Shutdown::Write));
1133 assert!(s.write(&[1]).is_err());
1135 assert_eq!(t!(s.read(&mut b)), 1);
1136 assert_eq!(b[0], 1);
1141 fn close_readwrite_smoke() {
1142 each_ip(&mut |addr| {
1143 let a = t!(TcpListener::bind(&addr));
1144 let (tx, rx) = channel::<()>();
1145 let _t = thread::spawn(move|| {
1146 let _s = t!(a.accept());
1151 let mut s = t!(TcpStream::connect(&addr));
1152 let mut s2 = t!(s.try_clone());
1154 // closing should prevent reads/writes
1155 t!(s.shutdown(Shutdown::Write));
1156 assert!(s.write(&[0]).is_err());
1157 t!(s.shutdown(Shutdown::Read));
1158 assert_eq!(s.read(&mut b).unwrap(), 0);
1160 // closing should affect previous handles
1161 assert!(s2.write(&[0]).is_err());
1162 assert_eq!(s2.read(&mut b).unwrap(), 0);
1164 // closing should affect new handles
1165 let mut s3 = t!(s.try_clone());
1166 assert!(s3.write(&[0]).is_err());
1167 assert_eq!(s3.read(&mut b).unwrap(), 0);
1169 // make sure these don't die
1170 let _ = s2.shutdown(Shutdown::Read);
1171 let _ = s2.shutdown(Shutdown::Write);
1172 let _ = s3.shutdown(Shutdown::Read);
1173 let _ = s3.shutdown(Shutdown::Write);
1179 #[cfg(unix)] // test doesn't work on Windows, see #31657
1180 fn close_read_wakes_up() {
1181 each_ip(&mut |addr| {
1182 let a = t!(TcpListener::bind(&addr));
1183 let (tx1, rx) = channel::<()>();
1184 let _t = thread::spawn(move|| {
1185 let _s = t!(a.accept());
1189 let s = t!(TcpStream::connect(&addr));
1190 let s2 = t!(s.try_clone());
1191 let (tx, rx) = channel();
1192 let _t = thread::spawn(move|| {
1194 assert_eq!(t!(s2.read(&mut [0])), 0);
1195 tx.send(()).unwrap();
1197 // this should wake up the child thread
1198 t!(s.shutdown(Shutdown::Read));
1200 // this test will never finish if the child doesn't wake up
1207 fn clone_while_reading() {
1208 each_ip(&mut |addr| {
1209 let accept = t!(TcpListener::bind(&addr));
1211 // Enqueue a thread to write to a socket
1212 let (tx, rx) = channel();
1213 let (txdone, rxdone) = channel();
1214 let txdone2 = txdone.clone();
1215 let _t = thread::spawn(move|| {
1216 let mut tcp = t!(TcpStream::connect(&addr));
1218 t!(tcp.write(&[0]));
1219 txdone2.send(()).unwrap();
1222 // Spawn off a reading clone
1223 let tcp = t!(accept.accept()).0;
1224 let tcp2 = t!(tcp.try_clone());
1225 let txdone3 = txdone.clone();
1226 let _t = thread::spawn(move|| {
1227 let mut tcp2 = tcp2;
1228 t!(tcp2.read(&mut [0]));
1229 txdone3.send(()).unwrap();
1232 // Try to ensure that the reading clone is indeed reading
1234 thread::yield_now();
1237 // clone the handle again while it's reading, then let it finish the
1239 let _ = t!(tcp.try_clone());
1240 tx.send(()).unwrap();
1241 rxdone.recv().unwrap();
1242 rxdone.recv().unwrap();
1247 fn clone_accept_smoke() {
1248 each_ip(&mut |addr| {
1249 let a = t!(TcpListener::bind(&addr));
1250 let a2 = t!(a.try_clone());
1252 let _t = thread::spawn(move|| {
1253 let _ = TcpStream::connect(&addr);
1255 let _t = thread::spawn(move|| {
1256 let _ = TcpStream::connect(&addr);
1265 fn clone_accept_concurrent() {
1266 each_ip(&mut |addr| {
1267 let a = t!(TcpListener::bind(&addr));
1268 let a2 = t!(a.try_clone());
1270 let (tx, rx) = channel();
1271 let tx2 = tx.clone();
1273 let _t = thread::spawn(move|| {
1274 tx.send(t!(a.accept())).unwrap();
1276 let _t = thread::spawn(move|| {
1277 tx2.send(t!(a2.accept())).unwrap();
1280 let _t = thread::spawn(move|| {
1281 let _ = TcpStream::connect(&addr);
1283 let _t = thread::spawn(move|| {
1284 let _ = TcpStream::connect(&addr);
1294 let name = if cfg!(windows) {"socket"} else {"fd"};
1295 let socket_addr = next_test_ip4();
1297 let listener = t!(TcpListener::bind(&socket_addr));
1298 let listener_inner = listener.0.socket().as_inner();
1299 let compare = format!("TcpListener {{ addr: {:?}, {}: {:?} }}",
1300 socket_addr, name, listener_inner);
1301 assert_eq!(format!("{:?}", listener), compare);
1303 let stream = t!(TcpStream::connect(&("localhost",
1304 socket_addr.port())));
1305 let stream_inner = stream.0.socket().as_inner();
1306 let compare = format!("TcpStream {{ addr: {:?}, \
1307 peer: {:?}, {}: {:?} }}",
1308 stream.local_addr().unwrap(),
1309 stream.peer_addr().unwrap(),
1312 assert_eq!(format!("{:?}", stream), compare);
1315 // FIXME: re-enabled bitrig/openbsd tests once their socket timeout code
1316 // no longer has rounding errors.
1317 #[cfg_attr(any(target_os = "bitrig", target_os = "netbsd", target_os = "openbsd"), ignore)]
1320 let addr = next_test_ip4();
1321 let listener = t!(TcpListener::bind(&addr));
1323 let stream = t!(TcpStream::connect(&("localhost", addr.port())));
1324 let dur = Duration::new(15410, 0);
1326 assert_eq!(None, t!(stream.read_timeout()));
1328 t!(stream.set_read_timeout(Some(dur)));
1329 assert_eq!(Some(dur), t!(stream.read_timeout()));
1331 assert_eq!(None, t!(stream.write_timeout()));
1333 t!(stream.set_write_timeout(Some(dur)));
1334 assert_eq!(Some(dur), t!(stream.write_timeout()));
1336 t!(stream.set_read_timeout(None));
1337 assert_eq!(None, t!(stream.read_timeout()));
1339 t!(stream.set_write_timeout(None));
1340 assert_eq!(None, t!(stream.write_timeout()));
1345 fn test_read_timeout() {
1346 let addr = next_test_ip4();
1347 let listener = t!(TcpListener::bind(&addr));
1349 let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
1350 t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1352 let mut buf = [0; 10];
1353 let start = Instant::now();
1354 let kind = stream.read(&mut buf).err().expect("expected error").kind();
1355 assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut);
1356 assert!(start.elapsed() > Duration::from_millis(400));
1361 fn test_read_with_timeout() {
1362 let addr = next_test_ip4();
1363 let listener = t!(TcpListener::bind(&addr));
1365 let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
1366 t!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1368 let mut other_end = t!(listener.accept()).0;
1369 t!(other_end.write_all(b"hello world"));
1371 let mut buf = [0; 11];
1372 t!(stream.read(&mut buf));
1373 assert_eq!(b"hello world", &buf[..]);
1375 let start = Instant::now();
1376 let kind = stream.read(&mut buf).err().expect("expected error").kind();
1377 assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut);
1378 assert!(start.elapsed() > Duration::from_millis(400));
1384 let addr = next_test_ip4();
1385 let _listener = t!(TcpListener::bind(&addr));
1387 let stream = t!(TcpStream::connect(&("localhost", addr.port())));
1389 assert_eq!(false, t!(stream.nodelay()));
1390 t!(stream.set_nodelay(true));
1391 assert_eq!(true, t!(stream.nodelay()));
1392 t!(stream.set_nodelay(false));
1393 assert_eq!(false, t!(stream.nodelay()));
1400 let addr = next_test_ip4();
1401 let listener = t!(TcpListener::bind(&addr));
1403 t!(listener.set_ttl(ttl));
1404 assert_eq!(ttl, t!(listener.ttl()));
1406 let stream = t!(TcpStream::connect(&("localhost", addr.port())));
1408 t!(stream.set_ttl(ttl));
1409 assert_eq!(ttl, t!(stream.ttl()));
1413 fn set_nonblocking() {
1414 let addr = next_test_ip4();
1415 let listener = t!(TcpListener::bind(&addr));
1417 t!(listener.set_nonblocking(true));
1418 t!(listener.set_nonblocking(false));
1420 let mut stream = t!(TcpStream::connect(&("localhost", addr.port())));
1422 t!(stream.set_nonblocking(false));
1423 t!(stream.set_nonblocking(true));
1426 match stream.read(&mut buf) {
1427 Ok(_) => panic!("expected error"),
1428 Err(ref e) if e.kind() == ErrorKind::WouldBlock => {}
1429 Err(e) => panic!("unexpected error {}", e),
1435 each_ip(&mut |addr| {
1436 let (txdone, rxdone) = channel();
1438 let srv = t!(TcpListener::bind(&addr));
1439 let _t = thread::spawn(move|| {
1440 let mut cl = t!(srv.accept()).0;
1441 cl.write(&[1,3,3,7]).unwrap();
1445 let mut c = t!(TcpStream::connect(&addr));
1446 let mut b = [0; 10];
1448 let len = c.peek(&mut b).unwrap();
1451 let len = c.read(&mut b).unwrap();
1454 t!(c.set_nonblocking(true));
1455 match c.peek(&mut b) {
1456 Ok(_) => panic!("expected error"),
1457 Err(ref e) if e.kind() == ErrorKind::WouldBlock => {}
1458 Err(e) => panic!("unexpected error {}", e),
1460 t!(txdone.send(()));