1234567891011121314151617181920212223242526272829303132333435363738394041424344454647(**********************************************************************)(* *)(* This file is part of the RFSM package *)(* *)(* Copyright (c) 2018-present, Jocelyn SEROT. All rights reserved. *)(* *)(* This source code is licensed under the license found in the *)(* LICENSE file in the root directory of this source tree. *)(* *)(**********************************************************************)letestablish_serverport=letopenUnixinletsockaddr=ADDR_INET(inet_addr_loopback,port)in(* Create TCP socket *)letsock=socketPF_INETSOCK_STREAM0insetsockoptsockSO_REUSEADDRtrue;bindsocksockaddr;listensock1;if!Options.verbosethenPrintf.printf"rfsm server: listening on 127.0.0.1:%d\n%!"port;(* Accept ONE client only *)let(client_sock,_)=acceptsockinif!Options.verbosethenPrintf.printf"rfsm server: client connected\n%!";letin_ch=in_channel_of_descrclient_sockinletout_ch=out_channel_of_descrclient_sockin(in_ch,out_ch)(* let rec loop () =
* match input_line in_ch with
* | line ->
* handler line out_ch;
* flush out_ch;
* loop () (\* continue *\)
* | exception End_of_file ->
* Printf.printf "server: client disconnected\n%!";
* ()
* in
* loop ();
* (\* Cleanup *\)
* close_in_noerr in_ch;
* close_out_noerr out_ch;
* (try close client_sock with _ -> ());
* Printf.printf "server: shutting down\n%!" *)letstart~socket_port~service=letic,oc=establish_serversocket_portinserviceicoc