On 03/08/2013 18:03, Kevin Cantu wrote:
> Inspired by your question, I'm now eyeballing what Servo does in the
> `rust-http-client` submodulel...
> https://github.com/mozilla-servo/rust-http-client

Thanks for the pointer. It does not compile for me (using v0.7), but
the source code was very useful. I rewrote my test code to use
non-blocking I/O, see below. (Presumably, the use of non-blocking I/O
sidesteps #3599.)

extern mod extra;
use extra::net::{ip, tcp};
use extra::net::tcp::TcpErrData;
use extra::uv;
use std::str;
use std::result;

fn main() {
    let ip_addr = ip::v4::parse_addr("204.232.212.130");
    let iotask = &uv::global_loop::get();

    let r = tcp::connect(ip_addr, 80, iotask);
    if (r.is_err()) {
        println(fmt!("Connection failed: %?", r.get_err()));
        return;
    }

    let socket = result::unwrap(r);

    let r = socket.write(bytes!("GET / HTTP/1.0\r\n\r\n").to_owned());  
    if (r.is_err()) {
        println(fmt!("Write error: %?", r.get_err()));
        return;
    }

    let r = socket.read_start();
    if (r.is_err()) {
        println(fmt!("Read error: %?", r.get_err()));
        return;
    }

    let read_port = r.get();

    loop {
        let r = read_port.recv();
        if (r.is_err()) {
            socket.read_stop();

            match r {
                result::Err(TcpErrData{err_name: ~"EOF", _}) => {
                    // Not an error; the end of stream.
                    return;
                }

                _ => {
                    // Genuine error.
                    println(fmt!("Read error: %?", r.get_err()));
                    return;
                }
            }
        }

        let bytes = r.get();
        println(str::from_bytes(bytes));
    }
}


> 
> 
> Kevin
> 
> 
> On Fri, Aug 2, 2013 at 1:10 AM, Ivan Ristić <[email protected]> wrote:
>> I am starting to play with Rust, but I got stuck early on with a trivial
>> TCP client example. (There's a few server examples out there, but I
>> couldn't find a single working client anywhere. I tried the archives,
>> the tests, etc.)
>>
>> My naive approach sends some data to the server and then attempts to
>> read, but socket.read() always times out. I have verified that the
>> server is receiving the request and responding to it.
>>
>> I came across a couple of tickets that suggest that I might be handling
>> the event loop incorrectly, but I don't know enough to fix the code.
>>
>> Your help is appreciated. Thanks.
>>
>> extern mod extra;
>> use extra::net::{ip, tcp};
>> use extra::uv;
>> use std::str;
>>
>> fn main() {
>>     let ip_addr = ip::v4::parse_addr("204.232.212.130");
>>     let iotask = &uv::global_loop::get();
>>
>>     let r = tcp::connect(ip_addr, 80, iotask);
>>     match(r) {
>>         Err(err) => {
>>                 println(fmt!("Connection failed: %?", err))
>>         }
>>
>>         Ok(socket) => {
>>                 let r = socket.write(bytes!("GET / 
>> HTTP/1.0\r\n\r\n").to_owned());
>>                 if (r.is_err()) {
>>                         println(fmt!("Write error: %?", r.get_err()))
>>                 } else {
>>                     let r = socket.read(2000);
>>                     if (r.is_err()) {
>>                             println(fmt!("Read error: %?", r.get_err()))
>>                     } else {
>>                             let bytes = r.get();
>>                             println(str::from_bytes(bytes));
>>                     }
>>             }
>>         }
>>     }
>> }
>>
>> --
>> Ivan
>> _______________________________________________
>> Rust-dev mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/rust-dev


-- 
Ivan
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to