How to reopen socket ? (Socket in use error)

J

Joe

Hi,

I browsed the news and a few seem to have this problem too, but no
solution !

I have a client server application where in case the connection gets bad
(crashes or whatever) client or server (who knows it first) closes the
socket and than wants to establish the SAME connection again ( due to
firewall reasons it has to be the SAME IP AND PORT).

Whatever I do, after close and trying to listen again, binding on the
local IPPort gets me the "connection in use error".

I use shutdownboth, close I use lingeroption with false and 0, I tried
to calle GC after close, I even tried to call socket.Dispose via reflection.

I can see in debug window, that socket is closed and cleaned up is set
to true and the local and remote endpoint properties are NULL, BUT there
is aa local ippoint member still active.

So how the hell can I clean up that socket to reconnect ??

I tried the UseSameAdress option, i dont get the socket in use
exception, but than no event is fired when the client connects (begin
accept delegate not fired), so obviously the clients seem to connect to
the dead socket somehow.

Any help please !

Joe Robe
 
B

BMermuys

Hi, [inline]

Joe said:
Hi,

I browsed the news and a few seem to have this problem too, but no
solution !

I have a client server application where in case the connection gets bad
(crashes or whatever) client or server (who knows it first) closes the
socket and than wants to establish the SAME connection again ( due to
firewall reasons it has to be the SAME IP AND PORT).

Do you mean the same ip/port on the server, on the client or both ?
Whatever I do, after close and trying to listen again, binding on the

Little confused here, why would you want to re-listen ??? A server creates
a listening socket and binds it to a local endpoint. You can call accept
more then once on the listening socket and each accepted socket has the same
local endpoint which is the local endpoint of the listener socket. So if an
accepted socket goes wrong just close the accepted socket and accept a new
one.
local IPPort gets me the "connection in use error".

I use shutdownboth, close I use lingeroption with false and 0, I tried
to calle GC after close, I even tried to call socket.Dispose via reflection.

I can see in debug window, that socket is closed and cleaned up is set
to true and the local and remote endpoint properties are NULL, BUT there
is aa local ippoint member still active.

So how the hell can I clean up that socket to reconnect ??

You can't. It is the OS that keeps the socket in a TIME_WAIT state after the
socket is closed. The maximum time is 3 minutes and depends on registry
settings.
I tried the UseSameAdress option, i dont get the socket in use
exception, but than no event is fired when the client connects (begin
accept delegate not fired), so obviously the clients seem to connect to
the dead socket somehow.

Don't know why this doesn't work for you, it seems like the listening socket
isn't really closed.

This works for me: closing the listening socket, create a new socket, set
the ReuseAddress option, bind to the same local endpoint and listen again.
I can get new accept events.

Hope this helps,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top