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.

fn clone_from(&mut self, source: &Self)

impl Reader for TcpStream

fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>

fn read_at_least(&mut self, min: usize, buf: &mut [u8]) -> IoResult<usize>

fn read_byte(&mut self) -> IoResult<u8>

fn push(&mut self, len: usize, buf: &mut Vec<u8>) -> IoResult<usize>

fn push_at_least(&mut self, min: usize, len: usize, buf: &mut Vec<u8>) -> IoResult<usize>

fn read_exact(&mut self, len: usize) -> IoResult<Vec<u8>>

fn read_to_end(&mut self) -> IoResult<Vec<u8>>

fn read_to_string(&mut self) -> IoResult<String>

fn read_le_uint_n(&mut self, nbytes: usize) -> IoResult<u64>

fn read_le_int_n(&mut self, nbytes: usize) -> IoResult<i64>

fn read_be_uint_n(&mut self, nbytes: usize) -> IoResult<u64>

fn read_be_int_n(&mut self, nbytes: usize) -> IoResult<i64>

fn read_le_uint(&mut self) -> IoResult<usize>

fn read_le_int(&mut self) -> IoResult<isize>

fn read_be_uint(&mut self) -> IoResult<usize>

fn read_be_int(&mut self) -> IoResult<isize>

fn read_be_u64(&mut self) -> IoResult<u64>

fn read_be_u32(&mut self) -> IoResult<u32>

fn read_be_u16(&mut self) -> IoResult<u16>

fn read_be_i64(&mut self) -> IoResult<i64>

fn read_be_i32(&mut self) -> IoResult<i32>

fn read_be_i16(&mut self) -> IoResult<i16>

fn read_be_f64(&mut self) -> IoResult<f64>

fn read_be_f32(&mut self) -> IoResult<f32>

fn read_le_u64(&mut self) -> IoResult<u64>

fn read_le_u32(&mut self) -> IoResult<u32>

fn read_le_u16(&mut self) -> IoResult<u16>

fn read_le_i64(&mut self) -> IoResult<i64>

fn read_le_i32(&mut self) -> IoResult<i32>

fn read_le_i16(&mut self) -> IoResult<i16>

fn read_le_f64(&mut self) -> IoResult<f64>

fn read_le_f32(&mut self) -> IoResult<f32>

fn read_u8(&mut self) -> IoResult<u8>

fn read_i8(&mut self) -> IoResult<i8>

impl Writer for TcpStream

fn write_all(&mut self, buf: &[u8]) -> IoResult<()>

fn write(&mut self, buf: &[u8]) -> IoResult<()>

fn flush(&mut self) -> IoResult<()>

fn write_fmt(&mut self, fmt: Arguments) -> IoResult<()>

fn write_str(&mut self, s: &str) -> IoResult<()>

fn write_line(&mut self, s: &str) -> IoResult<()>

fn write_char(&mut self, c: char) -> IoResult<()>

fn write_int(&mut self, n: isize) -> IoResult<()>

fn write_uint(&mut self, n: usize) -> IoResult<()>

fn write_le_uint(&mut self, n: usize) -> IoResult<()>

fn write_le_int(&mut self, n: isize) -> IoResult<()>

fn write_be_uint(&mut self, n: usize) -> IoResult<()>

fn write_be_int(&mut self, n: isize) -> IoResult<()>

fn write_be_u64(&mut self, n: u64) -> IoResult<()>

fn write_be_u32(&mut self, n: u32) -> IoResult<()>

fn write_be_u16(&mut self, n: u16) -> IoResult<()>

fn write_be_i64(&mut self, n: i64) -> IoResult<()>

fn write_be_i32(&mut self, n: i32) -> IoResult<()>

fn write_be_i16(&mut self, n: i16) -> IoResult<()>

fn write_be_f64(&mut self, f: f64) -> IoResult<()>

fn write_be_f32(&mut self, f: f32) -> IoResult<()>

fn write_le_u64(&mut self, n: u64) -> IoResult<()>

fn write_le_u32(&mut self, n: u32) -> IoResult<()>

fn write_le_u16(&mut self, n: u16) -> IoResult<()>

fn write_le_i64(&mut self, n: i64) -> IoResult<()>

fn write_le_i32(&mut self, n: i32) -> IoResult<()>

fn write_le_i16(&mut self, n: i16) -> IoResult<()>

fn write_le_f64(&mut self, f: f64) -> IoResult<()>

fn write_le_f32(&mut self, f: f32) -> IoResult<()>

fn write_u8(&mut self, n: u8) -> IoResult<()>

fn write_i8(&mut self, n: i8) -> IoResult<()>

impl AsRawFd for TcpStream

fn as_raw_fd(&self) -> Fd

impl AsRawFd for TcpStream

fn as_raw_fd(&self) -> Fd