2012-05-09 62 views
7

在這種情況下應該如何恢復?從「連接重置對等方」恢復Indy TCP客戶端

服務器崩潰,因此連接異常關閉。幾乎所有事件的調用都會導致「按對象重置連接」異常。我似乎已經通過在except塊內的TIdTCPClient對象上調用Disconnect來修復它,但是它會導致最後一個異常帶有相同的消息(我在第二個try-except塊中已經捕獲到)。

這是Indy10和Delphi XE2。

try 
     if not EcomSocket.Connected then EcomSocket.Connect(); 
    except 
     on e: Exception do begin 
     try 
      EcomSocket.Disconnect(); 
     except 
      MessageDlg('Connectivity to the server has been lost.', mtError, [mbOK], 0); 
     end; 
     end; 
    end; 
+0

你嘗試調試到TIdTCPConnection.Disconnect在IdTCPConnection.pas?也許一個OnDisconnect處理程序被執行並使用連接。或者NotifyPeer扮演一個角色,另請參閱源代碼中關於不同場景的註釋 – mjn

+0

釋放對象並創建一個新的對象將有所幫助(但無助於找到例外的原因) – mjn

+0

我並不太在意爲什麼調用斷開連接時出現異常。我只想知道如何從這樣的情況中妥善恢復。 –

回答

7

試試這個:

try 
    if not EcomSocket.Connected then EcomSocket.Connect(); 
except 
    try 
    EcomSocket.Disconnect(False); 
    except 
    end; 
    if EcomSocket.IOHandler <> nil then EcomSocket.IOHandler.InputBuffer.Clear; 
    MessageDlg('Connectivity to the server has been lost.', mtError, [mbOK], 0); 
end; 
+1

將「false」傳遞給Disconnect方法修復了它。它的默認行爲是調用DisconnectNotifyPeer,它觸發了第二個異常。 –