473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to reopen socket ? (Socket in use error)

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

Nov 15 '05 #1
1 6103
Hi, [inline]

"Joe" <fu**@rotz.de> wrote in message
news:us**************@TK2MSFTNGP12.phx.gbl...
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

Any help please !

Joe Robe

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
2
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
2
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
7
by: | last post by:
Hi all, I have a simple .aspx page running on net 2.0 that is trying to do a http post to a remote server. Here is the code Private Function ProcessRequests(ByVal strbody As String) As String...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
14
by: ahlongxp | last post by:
Hi, everyone, I'm implementing a simple client/server protocol. Now I've got a situation: client will send server command,header paires and optionally body. server checks headers and decides...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.