Working with ocaml Lwt sockets -


i have been on learning ocaml week, things got clear, others rather not. i'm trying compose simple tic-tac-toe server accepting connections via telnet. word. have use lwt , rigth seems me darken place because had faced of language pecularities. begining of code:

let sock = lwt_unix.socket unix.pf_inet unix.sock_stream 0  let setting_up_server_socket =       let sockaddr = (unix.addr_inet(unix.inet_addr_of_string "127.0.0.1", 23233)) in     lwt_unix.set_close_on_exec sock;     lwt_unix.setsockopt sock unix.so_reuseaddr true;     lwt_unix.bind sock sockaddr;     lwt_unix.listen sock 20 

ok, that's clear. server socket set listen clients connections. let's try first guest:

let handle_income =      lwt_unix.accept sock;; 

seems clear, but:

val handle_income : (lwt_unix.file_descr * lwt_unix.sockaddr) lwt.t = <abstr> 

here i'm stacked. don't know how send message client socket. unlike general unix.accept returns ('a * 'b) lwt.t. questions:

  1. how can pure client "lwt_unix.file_descr" that?
  2. ('a * 'b) lwt.t. tuple belonging lwt.t object, isn't it? or field? confused @ all.

p.s. sorry if english not acceptable. learning long time ago. question asked please give piece of advise, can best information ocaml. best mean short/clearness. due reading o'reilly instantly forget reading 5 min ago because it's never clear or author talking used for. ocaml quite different java , js had started with.

a value of type 'a lwt.t represents thread evaluate value of type 'a once ready (or error went wrong). way 'a 'a lwt.t bind function, called soon, 'a lwt.t thread finished , data ready. can bind lwt.bind function, available in infix notation >>=.

for example:

let process_client fd =    lwt_unix.accept fd >>= fun (cli,sock) ->   let chan = lwt_io.(of_fd ~mode:output cli) in   lwt_io.fprintf chan "hi there, , bye!" >>= fun () ->   lwt_io.close chan 

this uses infix notation, can rewrite more verbose:

let reply chan =    lwt_io.fprintf chan "hi there, , bye!"  let finish chan = lwt_io.close chan  let create_chan = lwt_io.(of_fd ~mode:output cli)  let process_client fd =    let accept_t = lwt_unix.accept fd in   let chan_t = lwt.map accept_t create_chan in   let reply_t = lwt.bind accept_t reply in    lwt.bind reply_t finish 

where _t designates thread. i.e., accept_t thread return pair of file descriptor , socket address. chan_t thread return channel (for doing buffered io).

lwt comes support special syntax. support 2 syntaxes, old camlp4 , newer 1 ppx. in old syntax, 1 can write process_client function follows:

let process_client fd =    lwt (cli,sock) = lwt_unix.accept fd in   let chan = lwt_io.(of_fd ~mode:output cli) in   lwt () = lwt_io.fprintf chan "hi there, , bye!" in   lwt_io.close chan 

there lwt special form of let not binds names values, waits them evaluate.

i hope answered questions. may find library interesting.

p.s. didn't test code, if have issues, don't hesitate ask.

p.p.s. left proper shutdown procedure, keep code simple.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -