Xt templating language (OCaml server) (http://chris.pacejo.net/programs/xt)

root / server.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
open Unix

let establish_server2 ?(backlog=64) ?(children=3) run sockaddr =
	let fd = socket (domain_of_sockaddr sockaddr) SOCK_STREAM 0 in
	bind fd sockaddr;
	listen fd backlog;
	let rec f i =
		if i = 0 || fork () = 0 then
			let r = run () in
			while true do
				let fd', _ = accept fd in
				let ic = in_channel_of_descr fd'
				and oc = out_channel_of_descr fd' in
				r ic oc;
				flush oc;
				close fd'
			done
		else f (i - 1)
	in f children