'."\n"; ?> ~/Documents/prog/ocaml/MLX/init-X.ml.html

type card = CARD8 | CARD16 | CARD32

let output_int o card v =
  begin
    match card with
    | CARD8 ->
        output_char o (char_of_int(v land 255))
    | CARD16 ->
        output_char o (char_of_int((v lsr 8) land 255));
        output_char o (char_of_int(v land 255))
    | CARD32 ->
        output_char o (char_of_int((v lsr 16) land 255));
        output_char o (char_of_int((v lsr 8) land 255));
        output_char o (char_of_int(v land 255))
  end;
  flush o;


type stringN = STRING8 | STRING16

let output_string o str_t str =
  begin
    match str_t with
    | STRING8 ->
        let len = String.length str in
        let rec aux i =
          if i < len then
          begin
            output_char o str.[i];
            aux(succ i)
          end
        in
        aux 0
    | STRING16 ->
        failwith "TODO: STRING16 implementation"
  end;
  flush o;


let () =
  let inet_addr = Unix.inet_addr_loopback in
  let sockaddr = Unix.ADDR_INET(inet_addr, 6000) in
  let ic, out_channel = Unix.open_connection sockaddr in

  let put_char c = output_char out_channel c ; flush out_channel in
  let put_int = output_int  out_channel in
  let put_string = output_string  out_channel in

  (* Big-endian: values are transmitted most significant byte first *)
  put_char 'B';
  (*
  (* little-endian: values are transmitted least significant byte first *)
  put_char 'l';
  *)

  put_int CARD16 11; (* protocol-major-version: X11 *)
  put_int CARD16  6; (* protocol-minor-version:  R6 *)

  put_string STRING8 "\000"; (* authorization-protocol-name *)
  put_string STRING8 "\000"; (* authorization-protocol-data *)

  let rec get_answer acc =
    try
      let c = input_char ic in
      get_answer(c::acc)
    with End_of_file -> acc
  in
  let answer = get_answer[] in

  List.iter (fun c -> print_char c) (List.rev answer);
  print_newline();

  close_in  ic;
  close_out_noerr  out_channel;