Created
August 7, 2013 06:24
-
-
Save Dubhead/6171695 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: SocketAddrのFromStrを使って書き直すこと。
see rust-lang/rust#8336