Struct std::old_io::net::tcp::TcpStreamUnstable
[-] [+]
[src]
pub struct TcpStream { // some fields omitted }
A structure which represents a TCP stream between a local socket and a remote socket.
The socket will be closed when the value is dropped.
Example
fn main() { use std::old_io::TcpStream; { let mut stream = TcpStream::connect("127.0.0.1:34254"); // ignore the Result let _ = stream.write(&[1]); let mut buf = [0]; let _ = stream.read(&mut buf); // ignore here too } // the stream is closed here }use std::old_io::TcpStream; { let mut stream = TcpStream::connect("127.0.0.1:34254"); // ignore the Result let _ = stream.write(&[1]); let mut buf = [0]; let _ = stream.read(&mut buf); // ignore here too } // the stream is closed here
Methods
impl TcpStream
fn connect<A: ToSocketAddr>(addr: A) -> IoResult<TcpStream>
Open a TCP connection to a remote host.
addr
is an address of the remote host. Anything which implements ToSocketAddr
trait can be supplied for the address; see this trait documentation for
concrete examples.
fn connect_timeout<A: ToSocketAddr>(addr: A, timeout: Duration) -> IoResult<TcpStream>
Creates a TCP connection to a remote socket address, timing out after the specified duration.
This is the same as the connect
method, except that if the timeout
specified elapses before a connection is made an error will be
returned. The error's kind will be TimedOut
.
Same as the connect
method, addr
argument type can be anything which
implements ToSocketAddr
trait.
If a timeout
with zero or negative duration is specified then
the function returns Err
, with the error kind set to TimedOut
.
fn peer_name(&mut self) -> IoResult<SocketAddr>
Returns the socket address of the remote peer of this TCP connection.
fn socket_name(&mut self) -> IoResult<SocketAddr>
Returns the socket address of the local half of this TCP connection.
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()>
Sets the nodelay flag on this connection to the boolean specified
fn set_keepalive(&mut self, delay_in_seconds: Option<usize>) -> IoResult<()>
Sets the keepalive timeout to the timeout specified.
If the value specified is None
, then the keepalive flag is cleared on
this connection. Otherwise, the keepalive timeout will be set to the
specified time, in seconds.
fn close_read(&mut self) -> IoResult<()>
Closes the reading half of this connection.
This method will close the reading portion of this connection, causing all pending and future reads to immediately return with an error.
Example
fn main() { #![allow(unused_must_use)] use std::old_io::timer; use std::old_io::TcpStream; use std::time::Duration; use std::thread; let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap(); let stream2 = stream.clone(); let _t = thread::spawn(move|| { // close this stream after one second timer::sleep(Duration::seconds(1)); let mut stream = stream2; stream.close_read(); }); // wait for some data, will get canceled after one second let mut buf = [0]; stream.read(&mut buf); }use std::old_io::timer; use std::old_io::TcpStream; use std::time::Duration; use std::thread; let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap(); let stream2 = stream.clone(); let _t = thread::spawn(move|| { // close this stream after one second timer::sleep(Duration::seconds(1)); let mut stream = stream2; stream.close_read(); }); // wait for some data, will get canceled after one second let mut buf = [0]; stream.read(&mut buf);
Note that this method affects all cloned handles associated with this stream, not just this one handle.
fn close_write(&mut self) -> IoResult<()>
Closes the writing half of this connection.
This method will close the writing portion of this connection, causing all future writes to immediately return with an error.
Note that this method affects all cloned handles associated with this stream, not just this one handle.
fn set_timeout(&mut self, timeout_ms: Option<u64>)
Sets a timeout, in milliseconds, for blocking operations on this stream.
This function will set a timeout for all blocking operations (including
reads and writes) on this stream. The timeout specified is a relative
time, in milliseconds, into the future after which point operations will
time out. This means that the timeout must be reset periodically to keep
it from expiring. Specifying a value of None
will clear the timeout
for this stream.
The timeout on this stream is local to this stream only. Setting a timeout does not affect any other cloned instances of this stream, nor does the timeout propagated to cloned handles of this stream. Setting this timeout will override any specific read or write timeouts previously set for this stream.
For clarification on the semantics of interrupting a read and a write,
take a look at set_read_timeout
and set_write_timeout
.
fn set_read_timeout(&mut self, timeout_ms: Option<u64>)
Sets the timeout for read operations on this stream.
See documentation in set_timeout
for the semantics of this read time.
This will overwrite any previous read timeout set through either this
function or set_timeout
.
Errors
When this timeout expires, if there is no pending read operation, no action is taken. Otherwise, the read operation will be scheduled to promptly return. If a timeout error is returned, then no data was read during the timeout period.
fn set_write_timeout(&mut self, timeout_ms: Option<u64>)
Sets the timeout for write operations on this stream.
See documentation in set_timeout
for the semantics of this write time.
This will overwrite any previous write timeout set through either this
function or set_timeout
.
Errors
When this timeout expires, if there is no pending write operation, no action is taken. Otherwise, the pending write operation will be scheduled to promptly return. The actual state of the underlying stream is not specified.
The write operation may return an error of type ShortWrite
which
indicates that the object is known to have written an exact number of
bytes successfully during the timeout period, and the remaining bytes
were never written.
If the write operation returns TimedOut
, then it the timeout primitive
does not know how many bytes were written as part of the timeout
operation. It may be the case that bytes continue to be written in an
asynchronous fashion after the call to write returns.
Trait Implementations
impl Clone for TcpStream
fn clone(&self) -> TcpStream
Creates a new handle to this TCP stream, allowing for simultaneous reads and writes of this connection.
The underlying TCP stream will not be closed until all handles to the stream have been deallocated. All handles will also follow the same stream, but two concurrent reads will not receive the same data. Instead, the first read will receive the first packet received, and the second read will receive the second packet.