Last active
March 25, 2019 22:10
-
-
Save thomaslee/4753338 to your computer and use it in GitHub Desktop.
Dumb "Hello World" TCP server in Rust.
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
use std::net; | |
use std::uv; | |
use core::pipes; | |
type ErrChan = pipes::SharedChan<Option<net::tcp::TcpErrData>>; | |
fn on_ready(kill_ch: ErrChan) { | |
io::println("listener is ready"); | |
} | |
fn on_connect(c: net::tcp::TcpNewConnection, kill_ch: ErrChan) { | |
do task::spawn() { | |
let res = net::tcp::accept(c); | |
if !res.is_err() { | |
let sck = result::unwrap(move res); | |
let s = ~"Hello World\r\n"; | |
sck.write(str::to_bytes(s)); | |
} | |
else { | |
kill_ch.send(Some(res.unwrap_err())); | |
} | |
} | |
} | |
fn main() { | |
let ip_addr = net::ip::v4::parse_addr("127.0.0.1"); | |
let iotask = &uv::global_loop::get(); | |
net::tcp::listen( | |
move ip_addr, 9123, 128, iotask, on_ready, on_connect); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not working at all