Skip to content

Instantly share code, notes, and snippets.

@Dubhead
Created August 7, 2013 06:24
Show Gist options
  • Save Dubhead/6171695 to your computer and use it in GitHub Desktop.
Save Dubhead/6171695 to your computer and use it in GitHub Desktop.
// simple HTTP client, based on
// https://mail.mozilla.org/pipermail/rust-dev/2013-August/005117.html
//
// Note: Envvar RUST_NEWRT=1 is required to compile and run this.
use std::rt::io::net::ip::{Ipv4Addr, SocketAddr};
use std::rt::io::net::tcp::TcpStream;
use std::rt::io::{Reader, Writer};
use std::str;
fn main() {
let mut stream = TcpStream::connect(
SocketAddr { ip: Ipv4Addr(204, 232, 212, 130), port: 80 }
).expect("failed to connect");
stream.write(bytes!("GET / HTTP/1.0\r\n\r\n").to_owned());
let mut buf = [0u8, ..2000];
match stream.read(buf) {
None => fail!("Read error"),
Some(bytes_read) => {
println(str::from_bytes(buf.slice_to(bytes_read)));
}
}
}
@Dubhead
Copy link
Author

Dubhead commented Aug 11, 2013

TODO: SocketAddrのFromStrを使って書き直すこと。
see rust-lang/rust#8336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment