More info concerning my original problem which is ...

My application uses a TTnCnx client object to connect (hundreds of times 
per day) to a remote device to retrieve data. On rare occasions the pair 
seem to get stuck in a connected state and stay there indefinitely. 

The normal procedure is to call SendMsg which calls Connect. 
OnConnection, write the outgoing message. When a response is received, 
call ClosePort. If no response, MsgTimeout. 

{As I write this I see one problem, I don't call Close in the timeout 
procedure. Yet, after several failures, I would still call 
NewTelnetClient which I would expect to get me out of the hung state}

See any other obvious issues? Any problem calling Free immediately after 
Close in FreeTelnetClient?

Mike






procedure TTLS350E.SendMsg(Msg: string);
begin
  try
    if assigned(TelnetClient) then 
begin                                          
      TxTCPBuffer := Msg;
      if TelnetClient.IsConnected = True then begin { connection is 
already active }
        WriteToTelnet(Msg);
      end else begin                     { got to get a connection }
        TelnetClient.Connect;
      end;
    end;
  except
    ShowNewMessage( 'Exception handled in TCP SendMsg');
  end;
end;

procedure TTLS350E.ClosePort;
begin
  TelnetClient.Close;
end;

procedure TTLS350E.MsgTimeout(Sender: TObject);
begin
  inc(PortRebuildCount);
  if PortRebuildCount >= TLS350_TIMEOUTS_RESET then begin
    PortRebuildCount := 0;
    NewTelnetClient;
  end;
end;

procedure TTLS350E.FreePort;     // This should only be called from Destroy
begin
  FreeTelnetClient;
end;

procedure TTLS350E.FreeTelnetClient;
var
  tmpTelnet: TTnCnx;
begin
  if assigned(TelnetClient) then begin    // if TCP client exists, kill it
    tmpTelnet := TelnetClient;
    tmpTelnet.Close;
    tmpTelnet.Free;
    TelnetClient := nil;
  end;
end;

  // The following function is called only after multiple
  // message failures.
                                         // build a new Telnet object
procedure TTLS350E.NewTelnetClient;
begin
  FreeTelnetClient;
  TelnetClient := TTnCnx.Create(Application.MainForm);
  TelnetClient.OnDataAvailable := TnCnxDataAvailable;
  TelnetClient.OnSessionClosed := TnCnxSessionClosed;
  TelnetClient.OnSessionConnected := TnCnxSessionConnected;
  TelnetClient.Port := IntToStr(MyConfig.GetPort);
  TelnetClient.Host := MyConfig.GetIP;
end;

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to