caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Raphael Bauduin <rblists@gmail.com>
To: Koen De Keyser <koen.dekeyser@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Beginner's question on Lwt usage
Date: Sun, 27 Nov 2016 19:59:39 +0100	[thread overview]
Message-ID: <CAONrwUHQpyo72_zDw1+HrWz_7-YCUoUjfQf+445jFwfuY+t0xQ@mail.gmail.com> (raw)
In-Reply-To: <CAP+7bo37i9FjdvKvEqCGErd6CzJb2W6G-wAFqo9iXeLdSx9HFQ@mail.gmail.com>

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

Thanks for your answer!

I get the same result with the following code. Note that I can open 2
connections and that the closing of the connection works as expected with a
'close connection' pattern (with one line only with a dot followed by an
empty line). It is just that I cannot close the second connection while the
first stays open, the 'close connection' pattern is not triggering the
close.

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
let server port =
  let inet_addr = inet_addr_any in
  let sockaddr = ADDR_INET (inet_addr, port) in
  let domain = domain_of_sockaddr sockaddr in
  let sock:Unix.file_descr = socket domain SOCK_STREAM 0 in
  Unix.bind sock sockaddr;
  listen sock 10;
  Lwt_main.run ( Lwt.return (accept_connection sock) )


On Sun, Nov 27, 2016 at 6:41 PM, Koen De Keyser <koen.dekeyser@gmail.com>
wrote:

> You need to run your lwt code within Lwt_main.run
>
> This starts the lwt scheduler. Otherwise your application just terminates
> once it has created the lwt thread (which is a simple Ocaml value).
>
> Koen
>
> On Nov 27, 2016 18:30, "Raphael Bauduin" <rblists@gmail.com> wrote:
>
>> 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
>>
>>
>>


-- 
Web database: http://www.myowndb.com
Free Software Developers Meeting: http://www.fosdem.org

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

  reply	other threads:[~2016-11-27 18:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-27 17:29 Raphael Bauduin
2016-11-27 17:41 ` Koen De Keyser
2016-11-27 18:59   ` Raphael Bauduin [this message]
2016-11-27 21:05     ` Anton Bachin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAONrwUHQpyo72_zDw1+HrWz_7-YCUoUjfQf+445jFwfuY+t0xQ@mail.gmail.com \
    --to=rblists@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=koen.dekeyser@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).