Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.46 KB | None | 0 0
  1. let use_chans chans=
  2.   let rec use_chans_in rest all =
  3.     match rest with
  4.     | [] -> let%lwt () = Lwt_unix.sleep 5.0 in
  5.             let%lwt () = Lwt_io.printf "will use_chans\n%!" in
  6.             let%lwt () = Lwt_io.flush Lwt_io.stdout in
  7.             use_chans_in all all
  8.     | h::t ->
  9.             let%lwt ()=Lwt_io.fprintf h "hello socket world\n" in
  10.             use_chans_in t all
  11.   in
  12.     use_chans_in chans chans
  13.  
  14.  
  15. let init_socket addr port count=
  16.   let rec init_socket_in add port count chans =
  17.   let inet_addr = (Unix.inet_addr_of_string addr) in
  18.   let sockaddr = Unix.ADDR_INET (inet_addr, port) in
  19.   let domain = Unix.domain_of_sockaddr sockaddr in
  20.   (*let sock = Lwt_unix.socket domain Lwt_unix.SOCK_STREAM 0 in*)
  21.   let sock = Lwt_unix.socket domain Unix.SOCK_STREAM 0 in
  22.   let%lwt () = Lwt_unix.connect sock sockaddr in
  23.   let outchan = (Lwt_io.of_fd ~mode:Lwt_io.output sock) in
  24.   let%lwt ()=Lwt_io.fprintf outchan "hello socket world\n" in
  25.  
  26.   if count==0 then begin
  27.     let%lwt () = Lwt_io.printf "READY! will use_chans\n%!" in
  28.     let%lwt () = Lwt_io.flush Lwt_io.stdout in
  29.     let%lwt () =  Lwt_unix.sleep 5.0 in
  30.     use_chans (outchan::chans)
  31.   end else begin
  32.     init_socket_in addr port (count-1) (outchan::chans)
  33.   end
  34.   in
  35.     init_socket_in addr port count []
  36.  
  37. let () =
  38.   let addr = Sys.argv.(1) in
  39.   let port = int_of_string(Sys.argv.(2)) in
  40.   let count= int_of_string(Sys.argv.(3)) in
  41.     Lwt_main.run (init_socket addr port count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement