From: Lindsay Smith
Subject: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <3AECB987.7E0B17BD@cs.otago.ac.nz>
I am learning to do socket programming in Allegro lisp and everything
works fine but I can't
seem to close the sockets properly.  Here is the function:

(defun socket-func nil
  (let* ((s (acl-socket:make-socket :connect :passive :local-port
9910))   // Makes the socket
  (sock-str (acl-socket:accept-connection
s))                             // Creates a stream when a connetion is
made
  (msg (read
sock-str)))
// Read from the stream
    (format t "~a was the message~%"
msg)                                  // Write out what came in
    (close
sock-str)
// Close the stream
    (close
s)
// Close the socket
    t))

The first time I run this function it works fine, and I can connect from
a different program and send a message.
But once this function exits, I can't run it again because it says that
the port is already in use.

Is (close s)  not enough to release the port?

Any advice is appreciated


Lindsay

From: Mitchell R Whorlow
Subject: Re: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <86k842okv6.fsf@mrw.res-hall.nwu.edu>
Lindsay Smith <······@cs.otago.ac.nz> writes:

> The first time I run this function it works fine, and I can connect
> from a different program and send a message.  But once this function
> exits, I can't run it again because it says that the port is already
> in use.
> 
> Is (close s)  not enough to release the port?

You might want to look at the :resuse-address keyword to make-socket.
Here's a description from Franz's web site:

:reuse-address sets the SO_REUSEADDR flag. This allows a particular
port to be reopened in :connect :passive mode even if there is an
existing connection for the port. This is very useful when debugging
a server program since without it you may have to wait up to a minute
after closing a particular port to reopen the same port again (due to
certain port-non-reuse requirements found in the TCP/IP protocol).

<URL:http://www.franz.com/support/documentation/6.0/doc/pages/operators/socket/make-socket.htm>
From: Espen Vestre
Subject: Re: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <w68zki3mbl.fsf@wallace.ws.nextra.no>
Lindsay Smith <······@cs.otago.ac.nz> writes:

> The first time I run this function it works fine, and I can connect from
> a different program and send a message.
> But once this function exits, I can't run it again because it says that
> the port is already in use.

At least on Solaris, Allegro seems to rely on the OS to clean up closed 
sockets, which may take some time.

Try to wait for a few minutes and see what happens.
Also (if you're using unix), you can see what's the status of the
port you're using with netstat, by doing e.g. a

netstat -na |grep 9910

I haven't investigated what the difference might be (partly because
I currently don't have access to ACL), but for some reason LispWorks 
seems to work better, it is able to open listening sockets immediately 
after they've been closed. 
-- 
  (espen)
From: Johannes Gr�dem
Subject: Re: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <lz7l02zn2s.fsf@wintermute.copyleft.no>
Espen Vestre <·····@*do-not-spam-me*.vestre.net> writes:

> I haven't investigated what the difference might be (partly because
> I currently don't have access to ACL), but for some reason LispWorks 
> seems to work better, it is able to open listening sockets immediately 
> after they've been closed. 

Sounds a bit like LispWorks sets SO_REUSEADDR, while ACL doesn't?

-- 
johs
From: Steven M. Haflich
Subject: Re: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <3AED814D.3BB50A9B@pacbell.net>
"Johannes Gr�dem" wrote:
> 
> Espen Vestre <·····@*do-not-spam-me*.vestre.net> writes:
> 
> > I haven't investigated what the difference might be (partly because
> > I currently don't have access to ACL), but for some reason LispWorks
> > seems to work better, it is able to open listening sockets immediately
> > after they've been closed.
> 
> Sounds a bit like LispWorks sets SO_REUSEADDR, while ACL doesn't?

ACL does not set it by default, but accepts it as a keyword to make-socket.

Many operating systems do not allow a passive socket port to be reused
immediately after being closed.  This is a feature of the OS socket
implementation and has nothing whatever to do with ACL (as was suggested
incorrectly) and would happen quite the same in an application written in
Lisp.  There are some very good reasons (mostly security) why an OS would
want to work this way, but these issues go beyond Lisp.  For one thing,
it prevents a masquerading program from starting immediately after a
legitimate server has closed down and accepting connections intended for
the legitimate server.

You can turn it off if it doesn't matter to your application, or
selectively for convenience while debugging, but some feel it is
important to require explicit request from the application programmer
before relaxing any security feature.
From: Johannes Gr�dem
Subject: Re: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <lzelu9l8e9.fsf@wintermute.copyleft.no>
"Steven M. Haflich" <·······@pacbell.net> writes:

>> Sounds a bit like LispWorks sets SO_REUSEADDR, while ACL doesn't?
> ACL does not set it by default, but accepts it as a keyword to make-socket.

So it's LispWorks that is doing it wrong, then.  Or maybe they're tired
of people asking about it, and decided to change the default behaviour.
It's supposedly one of the most asked questions about socket programming
on Usenet.

And I don't think anyone claimed ACLs behaviour was flawed.  I think ACL
does it the right way, explicitly requiring the programmer to ask for
SO_REUSEADDR to be set.

-- 
johs
From: Lindsay Smith
Subject: Re: How do I properly close Sockets in Allegro?
Date: 
Message-ID: <3AF0E571.E94C247D@cs.otago.ac.nz>
Actually, when I remembered to re-load the source file, it works fine.

*sheepish grin*

Lindsay.