From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id DF4877EE9C for ; Sun, 27 Nov 2016 18:29:31 +0100 (CET) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=rblists@gmail.com; spf=Pass smtp.mailfrom=rblists@gmail.com; spf=None smtp.helo=postmaster@mail-qk0-f180.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of rblists@gmail.com) identity=pra; client-ip=209.85.220.180; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rblists@gmail.com"; x-sender="rblists@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of rblists@gmail.com designates 209.85.220.180 as permitted sender) identity=mailfrom; client-ip=209.85.220.180; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rblists@gmail.com"; x-sender="rblists@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qk0-f180.google.com) identity=helo; client-ip=209.85.220.180; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rblists@gmail.com"; x-sender="postmaster@mail-qk0-f180.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AjQhxER27zj2x+UywsmDT+DRfVm0co7zxezQtwd8Z?= =?us-ascii?q?se0SKvad9pjvdHbS+e9qxAeQG96KsLQf16GP6fyocFdDyK7JiGoFfp1IWk1Nou?= =?us-ascii?q?QttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXdrXKo8DEdBAj0OxZr?= =?us-ascii?q?KeTpAI7SiNm82/yv95HJbQhFgDSwbal8IRi5ogndq9cajIR/Iast1xXFpWdFdf?= =?us-ascii?q?5Lzm1yP1KTmBj85sa0/JF99ilbpuws+c1dX6jkZqo0VbNXAigoPGAz/83rqALM?= =?us-ascii?q?TRCT6XsGU2UZiQRHDg7Y5xznRJjxsy/6tu1g2CmGOMD9UL45VSi+46ptVRTljj?= =?us-ascii?q?oMOTwk/2HNksF+jKxVrhG8qRJh34HZe5uaOOZkc67HYd8WWWhMU8BMXCJBGIO8?= =?us-ascii?q?aI4PAvIdM+lCt4n9p0EBogW6BQKxGe7g0CJIiWLr3a071eQqDAbL3BY7ENIKsX?= =?us-ascii?q?TUt8n1NLwJXOC6yanH1zTDb/dM1Tjh74jIdwksrPeRVrx+dsrRzFMgFwLDjliI?= =?us-ascii?q?sYzqJSiV1vgMs2eF9eZgUvivi2EhpgpsoTav3t8hhpfVio8R0FzJ9iV0zJwrKd?= =?us-ascii?q?GmVEJ3e8OoHZhMuyycKoB4WNktQ3tytyY/0rAGuYC0fCwNyJk/wh7Qcf2Hc4yR?= =?us-ascii?q?7hL6V+adPS50hHx4dL+8mxq+60egyur7Vsm71FZFsDBJncXLtnAIzxDT686HRe?= =?us-ascii?q?Vh/kq5xzqDywTe5vtHLE00j6bXNYAtzqIqmpYOsknPAjf6mEDsg6+XckUk9PKo?= =?us-ascii?q?6+PiYrj+vJ+TLIt0igDlMqQygcyyGus4Mg0UUGia/eSwzqHs/Ur8QLlSlP05jr?= =?us-ascii?q?HZsIzGJcQcvqO2HxVa0oMn6xqmCzem0c8YnWUcIVJeeBOHipDpNEvULPD5C/e/?= =?us-ascii?q?mVWsny1xy/DIJL2ySqnKe3PKlbOkebdm92ZdzhAyxJZR/cF6ELYEdcj6Qk/4ud?= =?us-ascii?q?iQJBYkPxG1xa6zEt9515gCQkqAB6aYNOXZtlretbFnGPWFeIJA4GW1EPMi/fO7?= =?us-ascii?q?1XI=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CrAAA9FztYhrTcVdFeg2gBAQEBAT06f?= =?us-ascii?q?QYHozQBAQaJfo4aJIdeB0MQAQEBAQEBAQEBAQESAQEBCAsLCR0wgjMYgjYRBBk?= =?us-ascii?q?BGx4DEgMGBzcCJAERAQUBV4gwAQMXCY8zjUmCCYEyPzKLUIFsGAUBH4MNBYNMC?= =?us-ascii?q?hknDVSDXwIGEoViiVhigjiCXQWaVIZIij6CQY1xiUIdhloTHoETNU4+CQsIDDK?= =?us-ascii?q?CYFeBaT00iHUBAQE?= X-IPAS-Result: =?us-ascii?q?A0CrAAA9FztYhrTcVdFeg2gBAQEBAT06fQYHozQBAQaJfo4?= =?us-ascii?q?aJIdeB0MQAQEBAQEBAQEBAQESAQEBCAsLCR0wgjMYgjYRBBkBGx4DEgMGBzcCJ?= =?us-ascii?q?AERAQUBV4gwAQMXCY8zjUmCCYEyPzKLUIFsGAUBH4MNBYNMChknDVSDXwIGEoV?= =?us-ascii?q?iiVhigjiCXQWaVIZIij6CQY1xiUIdhloTHoETNU4+CQsIDDKCYFeBaT00iHUBA?= =?us-ascii?q?QE?= X-IronPort-AV: E=Sophos;i="5.31,707,1473112800"; d="scan'208,217";a="201889210" Received: from mail-qk0-f180.google.com ([209.85.220.180]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 27 Nov 2016 18:29:30 +0100 Received: by mail-qk0-f180.google.com with SMTP id n204so119637385qke.2 for ; Sun, 27 Nov 2016 09:29:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=A4YFjTA86e+80s2TQifF6ksYiwWKQCb1PhY2s9GM018=; b=upMS1LeWsZzx1iS5hbZtp76ja/ljr+2NPo6MxTnN2X7GAIsxpE9vHyc69wHCqH5mJE oIIXzppQx8Z0KPVDJLeliI+HRfohmAaUzmdixsxDL2h97HUNNUlUtlLhxN/ZGEEs2SWT OU+ufRksMXyZbhsgxtgx8Xxty1eyFJr4YwDJn9Kai2GkDeLH8mlC+jOGOvBGX5tS9HFE WazcBE1S/wpOpthGu2fRuHc2PSOSC0Xt61TAo+pDo3diCHhnA3kd7FqIdybmsY6Biuwl nWZnS3dYVD1k/REb+da+4jF5R1OECLNEAt16qcEHamEUla9IFBeXhuw170JvinOqMWDg 8VQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=A4YFjTA86e+80s2TQifF6ksYiwWKQCb1PhY2s9GM018=; b=GA/SUhNe9pq4xTG+oWMFcnlc4UFoqeF1t08SiRL7at/ny2oqKjoUfPziA7PiPeRs7v sPBRWUErtu9OxuFpI/gBgPKz/ZfqJqpQEqE8SaoeTj6CNl5MbpYycFsT4SvgbDWRqOw3 K9WkZhQYbJx8ebMuxCHfiDW5vpmLBhEUgB5i8IwT950CNE3P8XcY3JW/6VURmFNlNV8N PGMsRVX0rj1Fx2kxiobGj8jlKF8g985CCnlltri7kAf4l0yWemWX6kPIEFgw/KsnOme1 XO/IHcLcCzBHkO3ySWS6tYbrf+NSkX9hNBFkTllhG603UcZFfBDtTHAJF74MKAl1tl8z Y+kA== X-Gm-Message-State: AKaTC03xNKj7Wo/BhCxAZ8rvLdBkDDEVqbOJc1PBvFKpeFk45Jp9JxfjGQG0dcTtoiDVVjBXEhyWhAkbjCLYIQ== X-Received: by 10.55.157.80 with SMTP id g77mr16845888qke.12.1480267769289; Sun, 27 Nov 2016 09:29:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.55.118.196 with HTTP; Sun, 27 Nov 2016 09:29:28 -0800 (PST) From: Raphael Bauduin Date: Sun, 27 Nov 2016 18:29:28 +0100 Message-ID: To: caml-list@inria.fr Content-Type: multipart/alternative; boundary=94eb2c0701808fa39405424bb22b Subject: [Caml-list] Beginner's question on Lwt usage --94eb2c0701808fa39405424bb22b Content-Type: text/plain; charset=UTF-8 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 --94eb2c0701808fa39405424bb22b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi,

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

<= div>let rec accept_connection sock:Unix.file_descr =3D
=C2=A0 let= socket_thread =3D Lwt.return ( Unix.accept sock ) in
=C2=A0 let = _ =3D Lwt.bind socket_thread
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0( = fun (fd, caller) ->
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0ignore(Lwt_io.printf "accepted\n%!");
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(*let _ =3D set_nonblock fd in*)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Lwt.return (readall f= d) >>=3D fun a ->
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0ignore(Lwt_io.printf "%s\n%!" a);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ignore(Lwt= .async (fun () -> Lwt.return (close fd) ) );
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Lwt.return 0;
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0); in
=C2=A0 =C2=A0accept_connection = sock

The complete code is at=C2=A0http://pastie.org/10971189 .
But it doesn't seem to work. The problems are:
- = nothing is printed on stdout
- when I open 2 connections, the sec= ond 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

--94eb2c0701808fa39405424bb22b--