2010-05-18 5 views
0

나는 Indy TCPServer와 TCPClient가있는 델파이 응용 프로그램이 있습니다. 각 연결을 식별하기 위해 AContext.Bindind.Handle을 사용합니다 (잘못 되었습니까?). 인디 TCP 서버 - 핸들 OnDisconnect가 이미 삭제 되었습니까?

그래서 내가 연결을 그리드 표시이있는 내가 해제 후 지울 수 :

procedure TfrmMain.serverIndyDisconnect(AContext: TIdContext); 
var I:Integer; 
begin 
for I := 0 to gridClients.RowCount - 1 do 
begin 
    if gridClients.Cells[0, I] = IntToStr(AContext.Binding.Handle) then 
    begin 
    gridClients.Rows[I].Delete(I); 
    end; 
end; 

WriteLogEntry('Connection closed... (' + AContext.Binding.PeerIP+')'); 
end; 

을하지만 분리 이벤트에서를, 핸들은 (그 어느 401xxxxx, allready 비어 그래서 마지막 정수).

아이디어가 있으십니까?

답변

4

사용중인 델피 또는 인디의 버전은 언급하지 않지만 D2010 및 인디 10.x는 다음을 포함합니다.

"AContext.Data"속성을 사용하여 클라이언트를 식별했습니다. 나는 보통 그곳에서 객체를 생성하고 연결 해제 이벤트가 발생할 때 그것을 놓는다.

뉴는 OnConnect() 코드 : 아래

procedure TfrmMain.serverIndyConnect(AContext: TIdContext); 
begin 
    AContext.Data := TMyObject.Create(NIL); 
    // Other Init code goes here, including adding the connection to the grid 
end; 

수정 OnDisconnect의() 코드 :

procedure TfrmMain.serverIndyDisconnect(AContext: TIdContext); 
var I:Integer; 
begin 
    for I := 0 to gridClients.RowCount - 1 do 
    begin 
    if gridClients.Cells[0, I] = IntToStr(AContext.Data) then 
    begin 
     gridClients.Rows[I].Delete(I); 
    end; 
end; 
WriteLogEntry('Connection closed... (' + AContext.Binding.PeerIP+')'); 
end; 
+0

좋은 조언. 항상 자신의 식별자를 사용하고 Indy의 내부 객체와 핸들을 ID로 사용하지 마십시오. –