caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Beginner's question on Lwt usage
@ 2016-11-27 17:29 Raphael Bauduin
  2016-11-27 17:41 ` Koen De Keyser
  0 siblings, 1 reply; 4+ messages in thread
From: Raphael Bauduin @ 2016-11-27 17:29 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1118 bytes --]

Hi,

After writing a small tcp server in ocaml, I wanted to make it work
asynchronously with Lwt.
I changed the function handling the accept on the socket to look like this:

let rec accept_connection sock:Unix.file_descr =
  let socket_thread = Lwt.return ( Unix.accept sock ) in
  let _ = Lwt.bind socket_thread
         ( fun (fd, caller) ->
             ignore(Lwt_io.printf "accepted\n%!");
             (*let _ = set_nonblock fd in*)
             Lwt.return (readall fd) >>= fun a ->
                 ignore(Lwt_io.printf "%s\n%!" a);
                 ignore(Lwt.async (fun () -> Lwt.return (close fd) ) );
                 Lwt.return 0;
         ); in
   accept_connection sock

The complete code is at http://pastie.org/10971189 .

But it doesn't seem to work. The problems are:
- nothing is printed on stdout
- when I open 2 connections, the second can only be closed after the first
has been closed.

In the mean time I've discovered Lwt_unix, but before looking at it, I'd
like to understand what's wrong in my current code. Can you spot errors in
the code? General advice is also welcome!

Thanks

Raphael

[-- Attachment #2: Type: text/html, Size: 1646 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-11-27 21:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-27 17:29 [Caml-list] Beginner's question on Lwt usage Raphael Bauduin
2016-11-27 17:41 ` Koen De Keyser
2016-11-27 18:59   ` Raphael Bauduin
2016-11-27 21:05     ` Anton Bachin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).