Article View: borland.public.cppbuilder.internet.socket
Article #4628Blocking Socket Help
From: mike
Date: Fri, 07 Jan 2005 15:55
Date: Fri, 07 Jan 2005 15:55
77 lines
3573 bytes
3573 bytes
I have been trying to understand how to properly close my Server Socket.
Heres my environment.
I have a Server Socket running in stThreadBlocking and I create each
connecting client thread using the OnGetThread event of the socket.
Now I may have 50 clients connected through this Server Socket and all seems
to work properly. Each client has been setup to connect in a persistent
manner, that is if the socket connection drops the client will continue to
try and connect on a periodic basis.
Now my problem is this: If I try and disconnect the socket from the server
side I can't get the socket to close because the clients try and connect
causing the server socket to start another thread. So I put in a flag to
tell the server socket to not create the thread, but this does not help as
the server socket still creates a connection. So I tried to close the
ServerSocket itself and then close the active threads but when I do this the
ServerSocket reports there are no active connections.
Anyway I am totally confused on how to properly close the Server Socket and
really need someone to point me in the right direction. I have included the
code below on how I am doing trying to do this.
void __fastcall TForm1::ServerSocket2GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
// this is my attempt to stop a new thread from being created if I have
requested a server side shutdown
// this flag is set through a button click
if ( ShuttingDownNow )
{
return ;
}
SocketThread = new TMyServerThread( false, ClientSocket )
;
}
// this gets run when the button to close the sockets gets clicked
void __fastcall TForm1::CloseAllSockets( void )
{
for( int i = 0; i < ServerSocket2->Socket->ActiveConnections ; i++ )
{
;
TServerClientWinSocket *ClientSocket =
static_cast<TServerClientWinSocket*>( ServerSocket2->Socket->Connections[
i ] ) ;
TMyServerThread *Thread =
static_cast<TMyServerThread*>( ServerSocket2->Socket->GetClientThread(
ClientSocket ) ) ;
ServerSocket2->Socket->Connections[ i ]->Close()
;
}
// this is here to stop the ServerSocket from accepting any more connections
ServerSocket2->Close();
}
Now the thread does get closed but by the time I have tried to close all the
sockets more threads more have started.
I sure hope someone can point me in the right direction.
Thanks
Mike
Technical Details
Message-ID:
<41deb491$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!transit.nntp.hccnet.nl!transit1.nntp.hccnet.nl!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,b1c5fb84bd800066,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "mike" <mfbrowne@earthlink.net> Newsgroups: borland.public.cppbuilder.internet.socket Subject: Blocking Socket Help Date: Fri, 7 Jan 2005 10:55:48 -0500 Lines: 79 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 68.204.189.169 Message-ID: <41deb491$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1105114257 68.204.189.169 (7 Jan 2005 08:10:57 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!transit.nntp.hccnet.nl!transit1.nntp.hccnet.nl!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail