Thread View: borland.public.cppbuilder.internet.socket
5 messages
5 total messages
Started by Maurice Anderson
Thu, 05 Jan 2006 08:00
TClientSocket won't disconnect safely from TServerSocket
Author: Maurice Anderson
Date: Thu, 05 Jan 2006 08:00
Date: Thu, 05 Jan 2006 08:00
43 lines
2171 bytes
2171 bytes
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
Author: Hans Galema
Date: Thu, 05 Jan 2006 09:49
Date: Thu, 05 Jan 2006 09:49
47 lines
2238 bytes
2238 bytes
Maurice Anderson wrote:
> ..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.
> 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();
> }
>
> }
> }
>
> 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
Author: Bob Gonder
Date: Thu, 05 Jan 2006 13:56
Date: Thu, 05 Jan 2006 13:56
8 lines
1358 bytes
1358 bytes
Maurice Anderson wrote: > 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.
Re: TClientSocket won't disconnect safely from TServerSocket
Author: Hans Galema
Date: Thu, 05 Jan 2006 14:23
Date: Thu, 05 Jan 2006 14:23
10 lines
1232 bytes
1232 bytes
Bob Gonder wrote: > Maurice Anderson wrote: > >> while (!Terminated ||ClientSocket->Connected) > Shouldn't that be && ? Yes. Hans.
Re: TClientSocket won't disconnect safely from TServerSocket
Author: Remy Lebeau (TeamB)
Date: Thu, 05 Jan 2006 18:15
Date: Thu, 05 Jan 2006 18:15
67 lines
3314 bytes
3314 bytes
"Maurice Anderson" <mauriceanderson@hotmail.com> wrote in message
news:43bcd080$1@newsgroups.borland.com...
> 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.
> 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();
}
}
> 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads