1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
(* -*- mode: sml -*-
* $Id: echo.sml,v 1.11 2003/09/24 17:45:27 sweeks Exp $
* http://www.bagley.org/~doug/shootout/
* from Tom 7
*)
exception Error of string
val data = "Hello there sailor\n"
val num = 100
val (port, listener) =
MLton.Socket.listen ()
handle _ => raise Error ("Can't listen...\n")
fun server () =
let val (_, _, ins, outs) = MLton.Socket.accept listener
fun s b =
case TextIO.inputLine ins of
NONE => let in
Posix.Process.wait ();
print (concat ["server processed ",
Int.toString b,
" bytes\n"])
end
| SOME i =>
let in
TextIO.output(outs, i);
TextIO.flushOut outs;
s (b + 19)
end
in s 0
end
fun client () =
let
val (ins, outs) = MLton.Socket.connect ("127.0.0.1", port)
fun c 0 = let in
TextIO.closeOut outs;
TextIO.closeIn ins
end
| c n = let in
TextIO.output(outs, data);
TextIO.flushOut outs;
TextIO.inputLine ins = SOME data
orelse raise Error "Didn't receive the same data";
c (n - 1)
end
in
c num
end
val _ = case Posix.Process.fork () of
SOME pid => server ()
| NONE => client ()
|