[rust-dev] Trying to get simple TCP server working

Michael Neumann mneumann at ntecs.de
Wed Dec 26 05:08:42 PST 2012


Hi,

I am trying to write a very simple TCP server (code given below), which 
just reads from a
socket, but somehow the read() blocks forever. Any idea what I am doing 
wrong?
I also tried to create a task for each new incoming connection, but the 
same happens.
I actually found this example on the internet...

When I do "wget http://127.0.0.1:4000/" it prints

   - Server is listening
   - New client
   - Accepted!
   - Unwrapped

and then it blocks forever. I also cannot create new connections at this 
point.

Best,

   Michael


/*
  * Simple TCP server
  */
extern mod std;
use std::net::tcp;
use std::net::ip;
use std::uv;

fn main() {
     tcp::listen(ip::v4::parse_addr("127.0.0.1"), 4000, 100, 
uv::global_loop::get(),
             |_comm_chan|{
                 error!("Server is listening");
             },
             |new_client, _comm_chan|{
               error!("New client");
                 let result = tcp::accept(new_client);
                 if result.is_ok(){
                     error!("Accepted!");
                     let socket = result::unwrap(move result);
                     error!("Unwrapped");
                     // Now do stuff with this socket
                     let data = socket.read(100); // XXX: This blocks
                     io::println(fmt!("%?", data));
                 }else{
                     error!("Not accepted!");
                 }
               } */
             });
}




More information about the Rust-dev mailing list