]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/contrib/otherlibs/labltk/examples_camltk/socketinput.ml
update
[l4.git] / l4 / pkg / ocaml / contrib / otherlibs / labltk / examples_camltk / socketinput.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                 MLTk, Tcl/Tk interface of Objective Caml            *)
4 (*                                                                     *)
5 (*    Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis    *)
6 (*               projet Cristal, INRIA Rocquencourt                    *)
7 (*            Jacques Garrigue, Kyoto University RIMS                  *)
8 (*                                                                     *)
9 (*  Copyright 2002 Institut National de Recherche en Informatique et   *)
10 (*  en Automatique and Kyoto University.  All rights reserved.         *)
11 (*  This file is distributed under the terms of the GNU Library        *)
12 (*  General Public License, with the special exception on linking      *)
13 (*  described in file LICENSE found in the Objective Caml source tree. *)
14 (*                                                                     *)
15 (***********************************************************************)
16 open Camltk
17
18 let _ =
19   let top_w = opentk () in
20   let text0_w = Text.create top_w [] in
21   let entry0_w = Entry.create top_w [] in
22   let button0_w = Button.create top_w
23                     [Text "Quit"; Command (fun _ -> exit 0)] in
24   let buffer = String.create 256 in
25   let master_socket = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
26   Unix.bind master_socket (Unix.ADDR_INET(Unix.inet_addr_any, 6789));
27   Unix.listen master_socket 3;
28   print_string "Please connect to port 6789..."; print_newline();
29   let (sock, _) = Unix.accept master_socket in
30   Fileevent.add_fileinput sock
31     (fun _ ->
32       let n = Unix.recv sock buffer 0 (String.length buffer) [] in
33       let txt = String.sub buffer 0 n in
34       Text.insert text0_w (TextIndex (End, [])) txt []);
35   let send _ =
36     let txt = Entry.get entry0_w ^ "\n" in
37     Entry.delete_range entry0_w (At 0) End ;
38     Unix.send sock txt 0 (String.length txt) [];
39     () in
40   bind entry0_w [([], KeyPressDetail "Return")] (BindSet ([], send));
41   pack [text0_w; entry0_w; button0_w][Side Side_Top; Fill Fill_X; Expand true];
42   mainLoop ()
43