From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 71C9FBB81 for ; Wed, 9 Nov 2005 14:02:45 +0100 (CET) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.200]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jA9D2ioO006606 for ; Wed, 9 Nov 2005 14:02:44 +0100 Received: by zproxy.gmail.com with SMTP id x3so168439nzd for ; Wed, 09 Nov 2005 05:02:44 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=fE+ODZNUtJIkbwPSU1JKPmR/0h5+uaHN47f7d42Q4YOvi1kn9oqzXn80bsh+mTpetb1hhnBYqkisCu2MU9BbmUvDj07rM9agemhZ0pfIxIo/i4RBD/cbEGLhP8rZZACIeYng5dgE9F1Di7HJhgBy8dQRa7GUe3bfVMt574UJJNs= Received: by 10.65.84.15 with SMTP id m15mr728638qbl; Wed, 09 Nov 2005 05:02:43 -0800 (PST) Received: by 10.65.160.14 with HTTP; Wed, 9 Nov 2005 05:02:43 -0800 (PST) Message-ID: Date: Wed, 9 Nov 2005 14:02:43 +0100 From: Maurizio Colucci Subject: Re: [Caml-list] Error binding socket Cc: caml-list In-Reply-To: <1131540939.844.9.camel@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <1131540939.844.9.camel@localhost.localdomain> X-Miltered: at concorde with ID 4371F374.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 gerd:01 stolpmann:01 gerd:01 rec:01 endline:01 endline:01 bug:01 sockets:01 howto:01 caml-list:01 beginner's:01 ocaml:01 beginners:01 bug:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_BY_IP autolearn=disabled version=3.0.3 Thank you, that's exactly the answer I was looking for! 2005/11/9, Gerd Stolpmann : > Am Mittwoch, den 09.11.2005, 13:33 +0100 schrieb Maurizio Colucci: > > Hi there. For my free tennis game (http://freetennis.sf.net), I get an > > error binding the socket to a port. (Unix_error 50, "bind"). The error > > description is "address already in use". > > > > This error only happens the *second* time I start the program. It is > > as if the port had not been freed and were still in use. The system is > > GNU/Linux Ubuntu Breezy. > > > > However, if I wait about 1minute, or I start the program specifying a > > different port, the problem does not happen. It is as if Linux were > > freeing the port with a delay. > > This is the default behaviour: Linux (and all other OS, too) has this > delay to ensure that pending connection attempts are properly reset. > This is reasonable for anonymous ports, but not for well-known service > ports. You can disable this with the socket option REUSEADDR. > > Gerd > > > > > At first glance, I thought I was not using Unix.close or Unix.socket > > correctly, but this does not seem to be the case. > > > > The code is freely available, however here is the relevant part of the = code: > > > > (* Here is the socket initialization phase, for both client and server:= *) > > > > let serverData =3D > > let rec tryToConnectNTimes n ~soc ~inet_a ~port=3D > > try > > Unix.connect soc (Unix.ADDR_INET (inet_a, por= t) ) > > with Unix.Unix_error _ -> > > if n =3D 0 then > > raise CouldNotConnectToServer > > else > > ( print_endline ( "The server is down. Retr= ying " ^ > > string_of_int (n-1) ^ " times."); > > Unix.sleep 1; > > tryToConnectNTimes (n-1) ~soc ~inet_a ~po= rt ) > > in > > if !server then > > let soc =3D Unix.socket Unix.PF_INET Unix.SOCK_STRE= AM 0 in > > ( > > (try > > Unix.bind soc (Unix.ADDR_INET (Unix.inet_ad= dr_any, !port)) > > with Unix.Unix_error (err, _, _) -> > > (print_endline ("Error: " ^(Unix.error_mess= age err) ^ ". This > > is a known bug. Please wait a few seconds or simply change the port > > number with the -port option"); > > exit 0 )); > > Unix.listen soc 5; > > print_endline "waiting for client to connect...= "; > > let clientSocket, _ =3D Unix.accept soc in > > Server ( (soc, clientSocket), Unix.in_channel_o= f_descr > > clientSocket, Unix.out_channel_of_descr clientSocket) > > ) > > > > else if 0 !=3D compare !client "" then > > let soc =3D Unix.socket Unix.PF_INET Unix.SOCK_STRE= AM 0 in > > let inet_a =3D Unix.inet_addr_of_string !client in > > print_endline "Connecting to server..."; > > ( tryToConnectNTimes 60 ~soc ~inet_a ~port:!port; > > print_endline "Connected to server"; > > Client (soc, Unix.in_channel_of_descr soc, Unix.= out_channel_of_descr soc) ) > > else > > NeitherServerNorClient > > in > > > > > > (*... and here is the socket cleanup code *) > > > > (match serverData with > > (* the rule is to shutdown before you close, > > but this is often automatic. see sockets > > howto. > > > > *) > > | Server( (sock, clientSocket), inc, outc) -> > > print_endline "Shutting down socket"; > > Unix.shutdown clientSocket Unix.SHUTDOWN_ALL ; > > Unix.shutdown sock Unix.SHUTDOWN_ALL ; > > Unix.close clientSocket; > > Unix.close sock; > > | Client ( sock, inc, outc) -> > > print_endline "Shutting down socket"; > > Unix.shutdown sock Unix.SHUTDOWN_ALL ; > > Unix.close sock; > > > > | NeitherServerNorClient -> ()); > > > > > > > > One doubt I have is the following: in the server code, must I call > > close on sock or in clientSocket? > > > > If I am doing something wrong, could you please tell me. :-) Thank you > > > > > > > > Maurizio > > > > _______________________________________________ > > Caml-list mailing list. Subscription management: > > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > > Archives: http://caml.inria.fr > > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > > Bug reports: http://caml.inria.fr/bin/caml-bugs > > > -- > ------------------------------------------------------------ > Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany > gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de > Telefon: 06151/153855 Telefax: 06151/997714 > ------------------------------------------------------------ > >