Board index » cppbuilder » TClientSocket won't disconnect safely from TServerSocket

TClientSocket won't disconnect safely from TServerSocket


2006-01-05 04:00:18 PM
cppbuilder45
Hello, My client is able to connect to my server with no problems. However,
if I do a ClientSocket1->Close() on the client, my server hangs. When I
check task manager, I can see that my server has pumped up CPU time way
high. If I force a close on the server, then I get a Dr. Watson error.
Here is my code in my TServerClientThread thread's Execute function:
void __fastcall TMyServerThread::ClientExecute(void)
{
try
{
pStream = new TWinSocketStream(ClientSocket, 6000);
}
catch (const Exception &E)
{
HandleException();
}
//BinaryTransfer1->ServerClientWinSocket = ClientSocket;
while (!Terminated ||ClientSocket->Connected)
{
if (pStream->WaitForData(6000))
{
}
}
}
This is my client's code:
void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket, TServerClientThread
*&SocketThread)
{
SocketThread = new TMyServerThread(true, ClientSocket);
SocketThread->FreeOnTerminate = true;
SocketThread->Priority = tpNormal;
SocketThread->Resume();
}
Please let me know if you see anything I am missing.
 
 

Re:TClientSocket won't disconnect safely from TServerSocket

Maurice Anderson wrote:
Quote
..If I force a close on the server, then I get a Dr. Watson error.
How do you force a close ? Dr Watson error ?
The server can determine when a client disconnects. This is after WaitForData
returns true and a successive Read returns 0.
Quote
Here is my code in my TServerClientThread thread's Execute function:

void __fastcall TMyServerThread::ClientExecute(void)

{
try
{
pStream = new TWinSocketStream(ClientSocket, 6000);
}
catch (const Exception &E)
{
HandleException();
}

//BinaryTransfer1->ServerClientWinSocket = ClientSocket;

while (!Terminated ||ClientSocket->Connected)
{
if (pStream->WaitForData(6000))
{
int n_read = pStream->Read ( ... );
if ( n_read == 0)
ClientSocket->Close();
Quote
}

}
}

This is my client's code:

void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket, TServerClientThread
*&SocketThread)
That does not very much look like client code.
Hans.
 

Re:TClientSocket won't disconnect safely from TServerSocket

Maurice Anderson wrote:
Quote
while (!Terminated ||ClientSocket->Connected)
Shouldn't that be && ?
You are staying in the loop until terminated, even if the connection
goes away. Since the connection is gone, the waitfor is probably
returning immediately, causing a tight loop.
 

{smallsort}

Re:TClientSocket won't disconnect safely from TServerSocket

Bob Gonder wrote:
Quote
Maurice Anderson wrote:

>while (!Terminated ||ClientSocket->Connected)
Shouldn't that be && ?
Yes.
Hans.
 

Re:TClientSocket won't disconnect safely from TServerSocket

"Maurice Anderson" < XXXX@XXXXX.COM >wrote in message
Quote
However, if I do a ClientSocket1->Close() on the client, my
server hangs. When I check task manager, I can see that my
server has pumped up CPU time way high.
Your thread code is failing to break out of its loop when a disconnect
happens. The TServerClientWinSocket::Connected property can (and usually
does) remain true in a blocking server, so your loop keeps running long
after the disconnect occurs. Also, the loop is running faster after the
disconnect because the TWinSocketStream::WaitForData() method is returning
immediately each time without any delay.
Quote
Here is my code in my TServerClientThread thread's Execute function:
When a graceful disconnect occurs, WaitForData() will return true and then
reading from the TWinSocketStream will return 0. That is the disconnect
notifiction that you need to check for in your code. Then code you showed
is not performing any reading.
Try this instead:
void __fastcall TMyServerThread::ClientExecute(void)
{
try
{
pStream = new TWinSocketStream(ClientSocket, 6000);
try
{
while( (!Terminated) && (ClientSocket->Connected) )
{
if( pStream->WaitForData(500) )
{
// read something ...
if( BytesRead <= 1 )
{
if( BytesRead == 0 )
// disconnected ...
else
// error ...
break;
}
}
}
}
__finally
{
delete pStream;
}
}
catch(const Exception &)
{
HandleException();
}
}
Quote
SocketThread->FreeOnTerminate = true;
Do not use FreeOnTerminate with TServerClientThread objects. TServerSocket
needs to maintain ownership of the thread memory at all times. And in fact,
will automatically reset FreeOnTerminate to false anyway.
Gambit