Skip to content

TCP server is not working #4296

Closed
Closed
@mneumann

Description

@mneumann

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!");
                }
              } */
            });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions