BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TClientSocket won't disconnect safely from TServerSocket

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Maurice Anderson
Guest





PostPosted: Thu Jan 05, 2006 8:00 am    Post subject: TClientSocket won't disconnect safely from TServerSocket Reply with quote



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.


Back to top
Hans Galema
Guest





PostPosted: Thu Jan 05, 2006 9:49 am    Post subject: Re: TClientSocket won't disconnect safely from TServerSocket Reply with quote



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.

Back to top
Bob Gonder
Guest





PostPosted: Thu Jan 05, 2006 1:56 pm    Post subject: Re: TClientSocket won't disconnect safely from TServerSocket Reply with quote



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.



Back to top
Hans Galema
Guest





PostPosted: Thu Jan 05, 2006 2:23 pm    Post subject: Re: TClientSocket won't disconnect safely from TServerSocket Reply with quote

Bob Gonder wrote:
Quote:
Maurice Anderson wrote:

while (!Terminated ||ClientSocket->Connected)

Shouldn't that be && ?

Yes.

Hans.

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Jan 05, 2006 6:15 pm    Post subject: Re: TClientSocket won't disconnect safely from TServerSocket Reply with quote


"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote


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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.