Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别_Delphi_Indy - Fatal编程技术网

Delphi TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别

Delphi TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别,delphi,indy,Delphi,Indy,我使用Indy 10.6.2.5298 TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别是什么?它们都会断开线路,但有时前者会出现访问冲突错误 很抱歉,我无法通过帮助文档及其源代码理解它 type TForm1 = class(TForm) IdTCPServer1: TIdTCPServer; procedure FormClick(Sender: TObject); procedure IdTCPServer1Ex

我使用Indy 10.6.2.5298

TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别是什么?它们都会断开线路,但有时前者会出现访问冲突错误

很抱歉,我无法通过帮助文档及其源代码理解它

type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    procedure FormClick(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
  private
    TestContext: TIdContext;
  end;

procedure TForm1.FormClick(Sender: TObject);
begin
  TestContext.Connection.Disconnect; // access violation

  TestContext.Connection.IOHandler.Close; // always works well
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
  TestContext := AContext;

  AContext.Connection.Disconnect; // works well
end;

TIdTCPConnection.Disconnect()
在内部调用
IOHandler.Close()
,如果
IOHandler
已分配并已打开(它还调用
TIdTCPConnection.DisconnectNotifyPeer()
并触发
OnDisconnected
OnStatus
事件):

TIdIOHandler.Close()
只需关闭套接字(如果已分配):

procedure TIdIOHandlerSocket.Close;
begin
  if FBinding <> nil then begin
    FBinding.CloseSocket;
  end;
  inherited Close;
end;
procedure TIdIOHandlerSocket.Close;
begin
  if FBinding <> nil then begin
    FBinding.CloseSocket;
  end;
  inherited Close;
end;
procedure TIdIOHandler.Close;
//do not do FInputBuffer.Clear; here.
//it breaks reading when remote connection does a disconnect
var
  // under ARC, convert a weak reference to a strong reference before working with it
  LIntercept: TIdConnectionIntercept;
begin
  try
    LIntercept := Intercept;
    if LIntercept <> nil then begin
      LIntercept.Disconnect;
    end;
  finally
    FOpened := False;
    WriteBufferClear;
  end;
end;
procedure TForm1.FormClick(Sender: TObject);
var
  List: TIdContextList;
begin
  List := IdTCPServer1.Contexts.LockList;
  try
    //has the context already been removed?
    if List.IndexOf(TestContext) <> -1 then
      TestContext.Connection.Disconnect;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
end;