有几个客户端向同一个UDPserver发送消息,我怎么只能监控到一个客户端的消息,要么是这一个 ,要么就是别一个,如何同时获取到多个呀,请高手指点一下!谢谢!

解决方案 »

  1.   

    LZ,你的现像是正常的!
    以下是UDPserver监听线程的代码,它是得到一个数据后就马上执行UDPRead。
    从而也就会出现你上述所说的情况!procedure TIdUDPListenerThread.Run;
    var
      PeerIP:string;
      i, PeerPort, ByteCount:Integer;
      FReadList:TList;
    begin
      FReadList := TList.Create;
      try
        FReadList.Capacity := FServer.Bindings.Count;
        for i := 0 to FServer.Bindings.Count - 1 do begin
          FReadList.Add(Pointer(FServer.Bindings[i].Handle));
        end;
        GStack.WSSelect(FReadList, nil, nil, AcceptWait);
        for i := 0 to FReadList.Count - 1 do try
          if not Stopped then begin
            IncomingData := FServer.Bindings.BindingByHandle(TIdStackSocketHandle(FReadList[i]));
            ByteCount := GStack.WSRecvFrom(IncomingData.Handle, FStream.Memory^, FBufferSize,
              0, PeerIP, PeerPort);
            GStack.CheckForSocketError(ByteCount);
            TCustomMemoryStream_Temp(FStream).SetPointer(FBuffer,ByteCount);
            FStream.Position := 0;
            IncomingData.SetPeer(PeerIP, PeerPort);
            if FServer.ThreadedEvent then begin
              UDPRead;
            end else begin
              Synchronize(UDPRead);
            end;
          end;
        except
        end;
      finally
        FReadList.Free;
      end;
    end;