现在我的TCPServer的工作线程都没有异常,但是当大量客户端以高速连接的时候,为什么停止TCPServer(ACtive := False;)的时候会报告线程停止错误?请指教。
急!在线等。

解决方案 »

  1.   

    你ACTIVE=FALSE的时候,线程没有被正常的断开或者销毁吧?
      

  2.   

    以下是工作线程代码
    procedure TARRequest.TCPServerExecute(AThread: TIdPeerThread);
    var
     flag:integer;
    begin
      {$IFDEF RELEASE}
      AThread.Connection.ReadTimeout:=FReadTimeOut;
      {$ENDIF}
      if not FStart then
      begin
        AThread.Connection.Disconnect;
        AThread.Terminate;
        exit;
      end;  try
      flag:=GetSysFlag(AThread.Connection);
      if flag=SysCommFlagClt_V1 then
         ClientRecieve(AThread)
      else
      if flag=SysCommFlagMAR_V1 then
         MARRecieve(AThread)
      else
      begin
         Raise Exception.Create('Illegal user');
      end;
      except
        AThread.Connection.Disconnect;
        //AThread.Terminate;
      end;
      if AThread.Connection.Connected then
         AThread.Connection.ReadLn();
    end; 在调试的时候没有报任何异常错误,但是仍然报错。nyf1220(我是党员)
     看看这里线程有没有被正常的断开或者销毁,如果没有,应该在哪里啊?----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------
      

  3.   

    呵呵
    Indy的Bug!!修改unit IdTCPServer;

    procedure TIdTCPServer.SetActive(AValue: Boolean);
    var
      i: Integer;
      LListenerThread: TIdListenerThread;
    begin
      // SG 28/11/01: removed the "try..finally FActive := AValue; end;" wrapper
      // SG 28/11/01: It cause the component to be locked in the "active" state, even if
      // SG 28/11/01: the socket couldn't be bound.
        if (not (csDesigning in ComponentState)) and (FActive <> AValue)
          and (not (csLoading in ComponentState)) then begin
          if AValue then begin
            // InitializeCommandHandlers must be called only at runtime, and only after streaming
            // has occured. This used to be in .Loaded and that worked for forms. It failed
            // for dynamically created instances and also for descendant classes.
            if not FCommandHandlersInitialized then begin
              FCommandHandlersInitialized := True;
              InitializeCommandHandlers;
            end;
            // Set up bindings
            if Bindings.Count = 0 then begin
              Bindings.Add;
            end;        // Set up ThreadMgr
            ThreadMgr.ThreadClass := ThreadClass;        // Setup IOHandler        if not Assigned(FIOHandler) then begin
              IOHandler := TIdServerIOHandlerSocket.Create(self);
              FImplicitIOHandler := true;
            end;
            // Update reply texts for "global" replies
            ReplyTexts.UpdateText(ReplyUnknownCommand);
            ReplyTexts.UpdateText(MaxConnectionReply);
            // Set up listener threads        IOHandler.Init;
            i := 0;
            try
              while i < Bindings.Count do begin
                with Bindings[i] do begin
                  AllocateSocket;
                  if (FReuseSocket = rsTrue) or ((FReuseSocket = rsOSDependent) and (GOSType = otLinux))
                    then begin
                    SetSockOpt(Id_SOL_SOCKET, Id_SO_REUSEADDR, PChar(@Id_SO_True), SizeOf(Id_SO_True));
                  end;
                  Bind;
                end;
                Inc(i);
              end;
            except
              Dec(i); // the one that failed doesn't need to be closed
              while i >= 0 do begin
                Bindings[i].CloseSocket;
                Dec(i);
              end;
              FActive := True;
              SetActive(False); // allow descendants to clean up
              raise;
            end;
            FListenerThreads := TThreadList.Create;
            for i := 0 to Bindings.Count - 1 do begin
              Bindings[i].Listen(FListenQueue);
              LListenerThread := TIdListenerThread.Create(Self, Bindings[i]);
              FListenerThreads.Add(LListenerThread);
              LListenerThread.Start;
            end;
            FActive := True;  // fixed by Liu Yang 2007.7.24      end else begin
            TerminateListenerThreads;
            FActive := false;  // fixed by Liu Yang 2007.7.24
            // Tear down ThreadMgr
            try
              TerminateAllThreads;
            finally
              if ImplicitThreadMgr and TIdThreadSafeList(Threads).IsCountLessThan(1) then begin // DONE -oAPR: BUG! Threads still live, Mgr dead ;-(
                FreeAndNil(FThreadMgr);
                FImplicitThreadMgr := False;
              end;
            end;//tryf
          end;
        end;
    end;
    就完美解决问题,适用于Indy9.0.17版,AtoZed.com可以下载的http://lysoft.7u7.net
      

  4.   

    你看看INDE的ACTIVE这个PROPERTY是怎么写的么,不是有代码么?看了就应该知道是哪里的线程冲突。
      

  5.   

    呵呵,是2004.7.24改的,不是2007.7.24SetActive的Bug就上面的处理方法
    其他使用最新的Indy9.0.17看看了,Delphi自带的Bug很多的http://lysoft.7u7.net
      

  6.   

    ly_liuyang(Liu Yang)
    能不能通过自身代码解决这个问题,不修改unit IdTCPServer
    谢谢:)----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------
      

  7.   

    已经解决,果然是INDY的BUG。
    谢谢各位:)结贴
    ----------------------------------------------------------------
                 花自飘零水自流,一种相思,两处闲愁。
                   此情无计可消除,才下眉头,又上心头。
    ----------------------------------------------------------------