By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

.NET Socket-ReConnection Error-Connecting To UnWorking Server Sock

P: n/a
First of all I wish you a good day.

My help request is about .NET asynchrounus socket communication. I have
developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I
have several problems re-establishin connection between peers. Below are my
problem cases after closing of the first successfull communication;

#1) I re-start the Server to accept connection requests. While it is
waiting, I run the Client. It is connecting to server socket and waits for
the data communication. But at this time, my server program is still waiting
at "BeginAccept". Nothing changes at its status.
#2) I re-start the Server to accept connection requests. While it is
waiting, I run the Client. It gives 10061 WSAECONNREFUSED (Connection refused
by host)
#3) After first successful communication closing, I start the Client to
connect to server 2-4 seconds later. It connects to a server socket however
my server socket program is not started to accept connection requests at this
time. Then, Client program waits for data communication.

I checked the working TCP sockets with "netstat -n -o 2" command. After the
closing of the first successfull communication, first server socket's status
either TIME_WAIT and several seconds later CLOSE_WAIT, or when it TIME_WAIT,
new connection is being set with same server IP:PORT with clietn socket
(problems at #1 and #3)

I shutdown and close my sockets using the same commands. Below is the
function, that is same for both;

static Socket *m_ServerSocket = NULL; // *m_ClientSocket = NULL;

System::Void mf_Shutdown_And_Close()
try {
if (m_ServerSocket != NULL)
if ( m_ServerSocket->Connected )

m_ServerSocket = NULL;

mf_ChangeButtonStatusTo( ConnStatus::CONNECT );
catch(Exception *exp) {
// Programda hata durumunu göster

Below are the options of my sockets
Server Socket:
SetSocketOption(..., SocketOptionName::ReuseAddress, true);
SetSocketOption(..., SocketOptionName::KeepAlive, true);
SetSocketOption(..., SocketOptionName::DontLinger, true);
Client Socket:
SetSocketOption(..., SocketOptionName::DontLinger, true );

Previously, I developed similar Server-Client programs using native C++
and blocking codes. I didnot faced with this kind of problems. Why Server
socket is not closing totally after succesfull Shutdown() and Close()

I put Thread::Sleep(..) to let the program give time to system to release
resources. Even Using GC::Collect() did not useful.

Thank you very much in advance for your helps.

PS: You may check my programs

I use
Windows XP Pro
Visual C++ .NET Standard v2003
..NET Framework 3.0
Windows Forms .NET application

İyi Çalışmalar / Wish You Good Work
Alper AKÇAYÖZ (Bil Muh)
Feb 21 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.