Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working code examples for std::rt::io::net #9829

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/libstd/rt/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@ Some examples of obvious things you might want to do

* Make an simple HTTP request

let socket = TcpStream::open("localhost:8080");
socket.write_line("GET / HTTP/1.0");
socket.write_line("");
let response = socket.read_to_end();
let s = TcpStream::connect(SocketAddr{ip:Ipv4Addr(127,0,0,1), port:8080});
let mut socket = s.unwrap();
socket.write(bytes!("GET / HTTP/1.0\n"));
socket.write(bytes!("\n"));
let mut buf = [0u8, ..2000];
let response = socket.read(buf);
if response.is_some() {
let optBytes = str::from_utf8_opt(buf);
if optBytes.is_some() {
println!("answer: {}", optBytes.unwrap());
}
}

* Connect based on URL? Requires thinking about where the URL type lives
and how to make protocol handlers extensible, e.g. the "tcp" protocol
Expand Down