A TCP connection
Example
use std::net::{NameLookup, TcpStream, Shutdown};
use std::io::{Result, StdioStream, copy};
fn http_fetch() -> Result<()> {
let addrs = NameLookup::resolve("docs.alumina-lang.net", 80)?;
defer addrs.free();
let socket = TcpStream::connect(&addrs.next().unwrap())?;
defer socket.close();
socket.write_all("GET / HTTP/1.1\r\n")?;
socket.write_all("Host: docs.alumina-lang.net\r\n")?;
socket.write_all("Connection: close\r\n\r\n")?;
socket.shutdown(Shutdown::Write)?;
copy(&socket, &StdioStream::stdout())?;
Result::ok(())
}
http_fetch().unwrap();
Run this example
Fields
-
socket: Socket
Methods
impl TcpStream { ... }
-
fn connect(addr: &SocketAddr) -> Result<TcpStream>
Establishes a TCP connection to a remote host.
-
Create a new TCP stream from a raw socket
-
fn as_fd(self: &TcpStream) -> FileDescriptor
-
fn peer_addr(self: &TcpStream) -> Result<SocketAddr>
Returns the address of the remote peer
-
fn socket_addr(self: &TcpStream) -> Result<SocketAddr>
Returns the address of the remote peer
-
Shuts down the reading or writing end of the socket or both.
-
Reads from the stream into a provided buffer.
If successful, returns the number of bytes read. This may be less than the size of the provided buffer.
-
Write the buffer to the stream.
Returns the number of bytes written. This may be less than the size of the buffer. See also write_all, which can be used to write the entire buffer.
-
Flush the stream
This may be a no-op, depending on the stream.
-
Closes the socket.
Mixins
impl TcpStream { ... }
-
-
Fills the provided buffer by reading from the stream.
If stream had less bytes available than the size of the buffer, this method will return an
Error::eof()
. -
Reads the entire stream into a
StringBuf
.If successful, returns the number of bytes that were read.
-
-
Writes all the bytes in the buffer to the stream.
If EOF is reached, returns
Error::eof()
.