From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 5C75D8005F for ; Sun, 27 Nov 2016 19:59:53 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=rblists@gmail.com; spf=Pass smtp.mailfrom=rblists@gmail.com; spf=None smtp.helo=postmaster@mail-qk0-f174.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of rblists@gmail.com) identity=pra; client-ip=209.85.220.174; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="rblists@gmail.com"; x-sender="rblists@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of rblists@gmail.com designates 209.85.220.174 as permitted sender) identity=mailfrom; client-ip=209.85.220.174; receiver=mail2-smtp-roc.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 (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qk0-f174.google.com) identity=helo; client-ip=209.85.220.174; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="rblists@gmail.com"; x-sender="postmaster@mail-qk0-f174.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AMtDXthHxIIOi5NchMyxJBJ1GYnF86YWxBRYc798d?= =?us-ascii?q?s5kLTJ75psuwAkXT6L1XgUPTWs2DsrQf2rGQ6PyrADdRqdbZ6TZZL8wKD0dEwe?= =?us-ascii?q?wt3CUeQ+e9QXXhK/DrayFoVO9jb3RCu0+BDE5OBczlbEfTqHDhpRQbGxH4KBYn?= =?us-ascii?q?br+tQt2ap42N2uuz45zeZRlTzHr4OOsqbUb+kQKElc4bh8NOJ+4SxwrSr2EAL+?= =?us-ascii?q?dRyGUuKV+Qkgfn692Y85tq8iAWsPUkoZ1uS6L/Kp4xVbxRATBuGWko6NDqs1GX?= =?us-ascii?q?VwGL52AASU0ZlxNJB07O6xSsDcS5iTfzqucogHrSBsbxV71hHG36t6o=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0BfAAAtLDtYhq7cVdFeGwEBAQMBAQEJA?= =?us-ascii?q?QEBFgEBAQMBAQEJAQEBgw0BAQEBAXdzCgYHpFqWeySCR4M2AoFfB0IRAQEBAQE?= =?us-ascii?q?BAQEBAQESAQEBCAsLCR0wgjMEARUBBIIWAQEBAwESEQQZARQHHQEDAQsGAwILD?= =?us-ascii?q?SoCAiEBAREBBQEcBhMiiDABAw8ICY86j1KBMj8yi1CBbBgFAR+DDQWDTAoZJw1?= =?us-ascii?q?UgzYBAQEBAQEBAwEBAQEBAQEYAgYSiweCSIFrgxqCXQWUa4U0NYZIhmiDVoJBj?= =?us-ascii?q?XGJQAIdhBKCSBMegRM0TxklCQsIDCERgxcggWk9NIcmgU8BAQE?= X-IPAS-Result: =?us-ascii?q?A0BfAAAtLDtYhq7cVdFeGwEBAQMBAQEJAQEBFgEBAQMBAQE?= =?us-ascii?q?JAQEBgw0BAQEBAXdzCgYHpFqWeySCR4M2AoFfB0IRAQEBAQEBAQEBAQESAQEBC?= =?us-ascii?q?AsLCR0wgjMEARUBBIIWAQEBAwESEQQZARQHHQEDAQsGAwILDSoCAiEBAREBBQE?= =?us-ascii?q?cBhMiiDABAw8ICY86j1KBMj8yi1CBbBgFAR+DDQWDTAoZJw1UgzYBAQEBAQEBA?= =?us-ascii?q?wEBAQEBAQEYAgYSiweCSIFrgxqCXQWUa4U0NYZIhmiDVoJBjXGJQAIdhBKCSBM?= =?us-ascii?q?egRM0TxklCQsIDCERgxcggWk9NIcmgU8BAQE?= X-IronPort-AV: E=Sophos;i="5.31,707,1473112800"; d="scan'208,217";a="246918078" Received: from mail-qk0-f174.google.com ([209.85.220.174]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 27 Nov 2016 19:59:41 +0100 Received: by mail-qk0-f174.google.com with SMTP id n204so120781718qke.2 for ; Sun, 27 Nov 2016 10:59:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=26/zjfcVckbx2ccsCIwFWgYaEPbCRAZ6kKbPeC5CoGY=; b=ejxMcAv4H+eYNr/JZE4867L6n4QSoxFHJtMenuqEp1VDm5LAqt+AE8BWALeJFRfBqu WdN35mcg1Ubbh+xkSavQNECCwFtVB28cUu3hkXMTRnj1WGoxnbv69VVmwluHD8sqEhPX v6VXv++mGwPdxGJE54KqlzQgCgHVKHLOPVLvncMJHi+vPDmIO4D1eogDibB4ITjLlnmd zz5ggIik69utsh1OZvLrKObFNnFl6m3eJGpFEZBY+ROsX07xaymuGDPZeH+SAyprwjic R9wofVyVREXF0HVDQyZlbPCI7dtiBKOvfU4a86k/vSr6LDCFX5Ko+Ds6UnX0Fluv5VLk 7sdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=26/zjfcVckbx2ccsCIwFWgYaEPbCRAZ6kKbPeC5CoGY=; b=LE9osDGSW8eCk+kO3xrbxjGBWxEX4FY5jfvGcMInqACgZRwq+Qo2izS2/RAJCynIGV 33SBAGp0y9arXvODwmIwsAcFpHZkpP+VEXNq08Di4WzFun/BxNEM2Egvocg6BN5v8JyK a9TS2tBB5R4DMYbp1BcaExuOARFjFb3ucEwD6cxCKKLfSs1yVFWeYSmTVP8RSZcMdka8 jWxHRbVpcJx6AajGYH/xZgR/jGwju745RitsqrWIxU9Tn6Z6bYgTOCKwh/2aInvwkAIR XuKWaZMb874n7DToays4CdRpQ5Qlc7EefbOqFinBghTaRAPW9+x+JYp+AnaPh1LuB64P SfFA== X-Gm-Message-State: AKaTC015MI2ABxLi0XktGH4cpWM3P5edQpBOhVs40V7ucvDMb2F/ztDbb3Yn3E+85j0F0IHv4Vs99wbcqtHj4w== X-Received: by 10.55.41.159 with SMTP id p31mr15029812qkp.212.1480273180171; Sun, 27 Nov 2016 10:59:40 -0800 (PST) MIME-Version: 1.0 Received: by 10.55.118.196 with HTTP; Sun, 27 Nov 2016 10:59:39 -0800 (PST) In-Reply-To: References: From: Raphael Bauduin Date: Sun, 27 Nov 2016 19:59:39 +0100 Message-ID: To: Koen De Keyser Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=001a1147b4ba13408c05424cf59b Subject: Re: [Caml-list] Beginner's question on Lwt usage --001a1147b4ba13408c05424cf59b Content-Type: text/plain; charset=UTF-8 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 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" 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 --001a1147b4ba13408c05424cf59b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
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 connecti= on' 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= =3D
=C2=A0 let socket_thread =3D Lwt.return ( Unix.accept sock )= in
=C2=A0 =C2=A0 let _ =3D Lwt.bind socket_thread
=C2= =A0 =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=A0 =C2=A0ignore(Lwt_io.printf= "accepted\n%!");
=C2=A0 =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=A0 =C2=A0Lwt.return (readall fd) >&g= t;=3D fun a ->
=C2=A0 =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=A0 =C2=A0ign= ore(Lwt.async (fun () -> Lwt.return (close fd) ) );
=C2=A0 =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 =C2=A0); in
=C2=A0 =C2= =A0 =C2=A0accept_connection sock
let server port =3D
= =C2=A0 let inet_addr =3D inet_addr_any in
=C2=A0 let sockaddr =3D= ADDR_INET (inet_addr, port) in
=C2=A0 let domain =3D domain_of_s= ockaddr sockaddr in
=C2=A0 let sock:Unix.file_descr =3D socket do= main SOCK_STREAM 0 in
=C2=A0 Unix.bind sock sockaddr;
= =C2=A0 listen sock 10;
=C2=A0 Lwt_main.run ( Lwt.return (accept_c= onnection sock) )


On Sun, Nov 27, 2016 at 6:41 PM, Koen De K= eyser <koen.dekeyser@gmail.com> wrote:
You need to run your lwt code within L= wt_main.run=C2=A0

This starts = the lwt scheduler. Otherwise your application just terminates once it has c= reated the lwt thread (which is a simple Ocaml value).=C2=A0

Koen

On Nov 27, 2= 016 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 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 .





--
=
--001a1147b4ba13408c05424cf59b--