caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Maurizio Colucci <maurizio.colucci@gmail.com>
Cc: caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Error binding socket
Date: Wed, 09 Nov 2005 13:55:18 +0100	[thread overview]
Message-ID: <1131540939.844.9.camel@localhost.localdomain> (raw)
In-Reply-To: <e919163f0511090433n77ab22acl@mail.gmail.com>

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
------------------------------------------------------------


  reply	other threads:[~2005-11-09 12:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-09 12:33 Maurizio Colucci
2005-11-09 12:55 ` Gerd Stolpmann [this message]
2005-11-09 13:02   ` [Caml-list] " Maurizio Colucci
2005-11-09 13:29 ` skaller
2005-11-09 14:36   ` Gerd Stolpmann

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=1131540939.844.9.camel@localhost.localdomain \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=maurizio.colucci@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).