I'm want to be able to re-connect to a idSimpleServer after one client connects to it and then disconnects. The first client can connect and disconnect no problem but the next client can't. I did a simple test procedure to demonstrate it my problem.

procedure Tfrmmain.btnBlockingClick(Sender: TObject);
begin
  Server1.BeginListen;
  Server1.Listen;
  CodeSite.Send(csmLevel2, 'Listen');
  CodeSite.Send(csmLevel2, 'Server1.IOHandler.Connected', Server1.IOHandler.Connected);
  try
    while (Server1.Connected) do
    begin
      while Server1.IOHandler.CheckForDataOnSource() do
      begin
        CodeSite.Send(csmLevel3, 'InputBufferAsString', Server1.IOHandler.InputBufferAsString);
        Server1.IOHandler.WriteLn('0006CANPDD');
      end;
    end;
  finally
    Server1.Disconnect;
    CodeSite.Send(csmLevel4, 'Finally');
  end;
end;

This give the following results in my codesite log:

Listen
Server1.IOHandler.Connected = True
Finally
Listen
Server1.IOHandler.Connected = False
Finally

Notice the second connection doesn't seem to bind the IOHandler properly. Not sure where I should be looking. Any ideas?

Thanks

Steve

有帮助吗?

解决方案

The problem is that you are reusing the same TIdSimpleServer object each time.

After the first disconnect, the same IOHandler is reused for the next connection, but the IOHandler.ClosedGracefully property remains true from the earlier connection becaue it is not being reset each time. The ClosedGracefully property is reset only by the IOHander.Open() method, which TIdSimpleServer calls only when it creates a new IOHandler. Disconnect() does not free the IOHandler, but it does call IOHandler.Close().

The missing call to Open() on subsequent connections looks like a bug to me, so I have checked in a fix for it to Indy's SVN (rev 5103).

You can either upgrade to the latest SVN release, or else you will have to destroy the TIdSimpleServer.IOHandler (or the TIdSimpleServer itself) in between each connection.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top