Ofcourse I didn't stop continue to look for what would be wrong. I think I'm
on to it now though! You're right in that GC is not related to the problem,
provided I'm on the right track.
But first, the reason why I don't use the reuse address option (Which can be
set in .NET with Socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true)) is that clients would still connect to
the old socket. At least, the clients failed connecting, while the new
socket reusing the address was up and listening.
But I discovered a bug in my client source. I'm running a test application
which has a window to interact with the server (start/stop etc) and a button
on it, that spawns a window with a client instance. An easy way to test a
server with multiple clients. After I had connected a couple of clients and
then stopped the server I looked at the results of netstat -a and saw there
were 3 sockets with status CLOSE_WAIT and 3 port maps with status
FIN_WAIT_2. This means that I just forgot to close client sockets when
remotely disconnected. (lol). However, I did set the client socket
references to null. Hence why the GC seemed to fix the problem; it cleaned
up the 3 client sockets, finally closing them.
So, I'll have to revise the close 'n disconnect code of the client a little
more. It will solve the problem :) If I wouldn't have found the problem now,
I would have with your reply, as that would have pointed me towards using
netstat.
Thanks anyways!
----- Original Message -----
From: "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com>
Newsgroups: microsoft.public.dotnet.framework
Sent: Saturday, April 21, 2007 6:49 PM
Subject: Re: Socket Problem: Exclusive Bind
[quoted text, click to view] > On Sat, 21 Apr 2007 08:35:06 -0700, MagicBox <avh^at^runbox.com> wrote:
>
>> [...]
>> This altogether means, that the garbage collector cleans up something of
>> the previously bound sockets which the socket's Close() method does not.
>> What
>> gives? On Win32 I never had this problem. When I closed a socket there, I
>> could instantly rebind to it.
>
> Actually, what it means is that garbage collection has nothing to do with
> your problem. Otherwise, it would have been resolved the first time you
> collected garbage. Why you didn't run into this issue using Winsock, I
> don't know.
>
> Look up "TIME_WAIT". This is a classic issue people run into when they
> use sockets and want to be able to recreate a socket with an identical
> address immediately after closing the socket. I don't know if the .NET
> Socket class has an equivalent to the "SO_REUSEADDR" socket option, but if
> it does you can set that and that will allow you to reuse the same socket
> address before the previous socket has been released by the system.
>
> Pete