473,763 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6136
Hi, [inline]

"Joe" <fu**@rotz.de > wrote in message
news:us******** ******@TK2MSFTN GP12.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
4638
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 and then fires a datareceived event. Within the waitingloop there is a timeout function, but I want the the 'last-time-socket-used' variable set when the socket is finished sending. When I send by System.Net.Sockets.Socket.Send(buffer()) (<--this...
2
702
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 more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
2
2879
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 is to make a server, none of us know nothing about socket programming in C#, but we founds some guides for this, and now ,got a server running, but we have some problems though. We have 2 scenario, one where we use a telnet connection and one...
7
2934
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 Dim returnstr As String Dim URL As String = "http://www.dydomain.com/test.asp"
1
10066
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. This program can perform 5 options from a menu on your POP3 mail by logging in with the correct POP3 server along with a user name and password that you use to log in to your ISP. The user name and password as well as the server name are all hard-coded...
11
7726
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 the app, even the first time. The application will be required to be sending out repeated emails, about one every second or two. Must this be done asynchronously? Thank you. -Greg I get the generic error messages;
10
3582
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. File "D:\Python24\lib\socket.py", line 295, in read data = self._sock.recv(recv_size) timeout: timed out
14
12794
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 whether to accept(read) the body. if server decided to throw(dump) the request's body, it'll send back a response message, such as "resource already exists" and close the connection.
2
3673
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 smoothly without any errors but,sometimes I get the following error. "An existing connection was forcibly closed by the remote host." Why is this happening when i am not closing the client program? Could someone guide me in this issue
0
9566
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.