Login  
Search All Forums
Dart Home | PowerTCP Emulation for ActiveX | Custom Development Reply | PowerTCP Emulation / Telnet for ActiveX Topics | Forums   
AuthorForum: PowerTCP Emulation / Telnet for ActiveX
Topic: Cyclic reconnect in thread
summj

From: Ettlingen, Germany
Posts: 3
Member Since: 06/22/06
posted July 25, 2006 8:56 AM

Hello,

I'm using your ActiveX Telnet Tool in a VC++ 6.0 Telnet-Client-Application.
After a Server-Disconnect, I try to start a cyclic reconnection in a Thread.
The problem is, the Thread doesn't end. Do you have any ideas ???
Here is the important part of my code, who should manage this.

##########################################################################


// Connecting via Telnet
int CTelnet::Connect()
{
  int nret = 0;
 
  // Return if already connected
  if (pTelnet->GetState() == DartTelnet::tcpConnected) {
   return 0;
  }
 
  if (m_strRemoteHost.IsEmpty()) {
   AfxMessageBox("Error: No Host address available for connecting");
   return -1;
  }

  // Settings
  pTelnet->PutTimeout(3000); //msec
  pTelnet->PutAutoOption(true);
  pTelnet->PutTermType("");

  // Connect
  try
  {
   nret = pTelnet->Connect( _bstr_t(m_strRemoteHost), m_nPort, _bstr_t(""), 0);
  }
  catch(_com_error e)
  {
   //CString strErr;
   //strErr.Format("ERROR #%d: %s",e.HelpContext(),(char *)e.Description());
   //AfxMessageBox(strErr);
  }
  catch(...)
  {
 
  }

  if (pTelnet->GetState() != DartTelnet::tcpConnected) {
   nret = 1;
  }

  return nret;
}
//----------------------------------------------------------------------


// Event, if Telnet state changes
void CTelnet::Telnet_State()
{
  int nTelnetState = pTelnet->GetState();
 
  switch (nTelnetState)
  {
   case DartTelnet::tcpClosed:   if (m_bDoReconnect)
                   {
                     // Start thread for reconnecting 
                    StartConnectThread();
                   }
                   break;
   case DartTelnet::tcpConnecting:
                   break;
   case DartTelnet::tcpConnected: 
                    
                   break;
   case DartTelnet::tcpClosing:  
                   break;
   default:            
                   break;
  }
}  
//-------------------------------------------------------------------------


// Starts Thread for reconnection
void CTelnet::StartConnectThread()
{
 
  if (m_pConnectThread)
  {
   // Waiting for Thread end
   WaitForSingleObject( m_pConnectThread->m_hThread, INFINITE);
   Sleep(100);
   delete m_pConnectThread
   m_pConnectThread = NULL;
  }
 
 
  // Thread starten ................................... :
  if ( !m_pConnectThread)
  {
   m_bConnectThreadRun = true;
   m_pConnectThread = AfxBeginThread( ConnectThread, this , THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
   m_pConnectThread->m_bAutoDelete = true; 
   m_pConnectThread->ResumeThread();    
  }
  // ....................................................

}
//------------------------------------------------------------------


// Threadfunction for connecting
UINT CTelnet::ConnectThread(LPVOID pParam) {
  int   nerr = 0;

  CTelnet* pthis = (CTelnet *)pParam;
   
  TRACE("CTelnet: ConnectThread started\n");
 
  try
  {
   nerr = pthis->Connect();
  }
  catch(...) {
   AfxMessageBox("Unknown Exception in CTelnet::ConnectThread. Cancel!");
   nerr = 1;
  }

  TRACE("CTelnet::ConnectThread() Thread ends\n");
 
 
  return nerr; // ExitCode
}
//----------------------------------------------------------------------
############################################################################
Tony Priest



From: Utica, NY USA
Posts: 8466
Member Since: 04/11/00
posted July 25, 2006 10:51 AM

You appear to be mixing blocking and non-blocking usage improperly. When you set the timeout to 3000 you are putting the control into blocking mode. When you get the state event, you are calling a blocking connect inside the event. The problem with this approach is that the event will not exit until connection is establish, but since you are inside the event, no other events fire so we don't receive notification.

Try changing your code so that connect is called with timeot = 0, then use the state event to let your program know that connection succeedes.

summj

From: Ettlingen, Germany
Posts: 3
Member Since: 06/22/06
posted July 26, 2006 6:05 AM

Hi Tony,

many thanks for you soon reply. With a timeout of 0 it works without thread. I needed a timeout > 0 to detect inside my Connect-function if it's succeed or not. Now I have inserted a MsgWait-loop after calling pTelnet->Connect and everthing seems to work fine.

greetings...
Reply | PowerTCP Emulation / Telnet for ActiveX Topics | Forums   
This site is powered by PowerTCP WebServer Tool PowerTCP WebServer for ActiveX