-
-
Save b0oh/dbf72d3c9bc86d94e4de 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
module EchoServer | |
let start () = listen 1234 | |
let listen port = | |
let (:ok, socket) = gen_tcp.listen port [(:packet, 0), (:active, false)] | |
in accept socket | |
let accept listen_socket = | |
let (:ok, socket) = gen_tcp.accept listen_socket | |
in do | |
spawn fn () -> echo socket | |
accept listen_socket | |
end | |
let echo socket = | |
match gen_tcp.recv socket 0 with | |
(:ok, data) -> | |
gen_tcp.send socket data | |
echo socket | |
(:error, :closed) -> | |
:ok | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant code.