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 A5A20BB81 for ; Wed, 9 Nov 2005 13:56:24 +0100 (CET) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.173]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jA9CuOQS005542 for ; Wed, 9 Nov 2005 13:56:24 +0100 Received: from [212.227.126.160] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1EZpV1-0002tM-00; Wed, 09 Nov 2005 13:56:23 +0100 Received: from [84.58.165.120] (helo=gate.lan.gerd-stolpmann.de) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1EZpV0-0004Ba-00; Wed, 09 Nov 2005 13:56:22 +0100 Received: from flakew.lan.gerd-stolpmann.de (flakew.lan.gerd-stolpmann.de [192.168.0.32]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id 3BD34C089; Wed, 9 Nov 2005 13:56:21 +0100 (CET) Subject: Re: [Caml-list] Error binding socket From: Gerd Stolpmann To: Maurizio Colucci Cc: caml-list In-Reply-To: References: Content-Type: text/plain Date: Wed, 09 Nov 2005 13:55:18 +0100 Message-Id: <1131540939.844.9.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:a6865a839c0178d9aa0ce41878507ea2 X-Miltered: at concorde with ID 4371F1F8.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=none autolearn=disabled version=3.0.3 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 = > let rec tryToConnectNTimes n ~soc ~inet_a ~port= > try > Unix.connect soc (Unix.ADDR_INET (inet_a, port) ) > with Unix.Unix_error _ -> > if n = 0 then > raise CouldNotConnectToServer > else > ( print_endline ( "The server is down. Retrying " ^ > string_of_int (n-1) ^ " times."); > Unix.sleep 1; > tryToConnectNTimes (n-1) ~soc ~inet_a ~port ) > in > if !server then > let soc = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in > ( > (try > Unix.bind soc (Unix.ADDR_INET (Unix.inet_addr_any, !port)) > with Unix.Unix_error (err, _, _) -> > (print_endline ("Error: " ^(Unix.error_message 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, _ = Unix.accept soc in > Server ( (soc, clientSocket), Unix.in_channel_of_descr > clientSocket, Unix.out_channel_of_descr clientSocket) > ) > > else if 0 != compare !client "" then > let soc = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in > let inet_a = 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 ------------------------------------------------------------