.NET Socket-ReConnection Error-Connecting To UnWorking Server Sock
Hello,
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->Shutdown(SocketShutdown::Both);
Thread::Sleep(250);
}
m_ServerSocket->Close();
m_ServerSocket = NULL;
Thread::Sleep(500);
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);
and
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()
commands?
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
www.AlperAkcayoz.com/PRG/AsynchSoket-CppNET.zip
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)
www.AlperAkcayoz.com