2014-09-10 108 views
1

我一直在使用印UDP,我想移动到印TCP转换自印UDP协议TCP

,但我不知道如何代码以同样的方式与印TCP工作转换

我的项目工作流发送这里聊天室是udp的代码

procedure TForm1.recorderData(Sender: TObject; const Buffer: Pointer; 
    BufferSize: Cardinal; var FreeIt: Boolean); 
begin 
Freeit :=True; 
if IDUDPCLIENT.active then 
IDUDPCLIENT.SendBuffer(RawToBytes(Buffer^, Buffersize)) 
else 
stop.Caption := 'error'; 
end; 

,这是读事件的服务器

procedure TForm1.UDPReceiverUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle); 
var 
    AudioDataSize: Integer; 
    AudioData : Pointer; 
begin 
    try 
    EnterCriticalSection(Section); 
    try 
     AudioDataSize := Length(AData); 
     if AudioDataSize > 10 then 
     begin 
     try 
      if not Player.Active then 
      begin 
      Player.Active := True; 
      Player.WaitForStart; 
      end; 
     except 
     end; 
     if BlockAlign > 1 then Dec(AudioDataSize, AudioDataSize mod BlockAlign); 
     AudioData := AudioBuffer.BeginUpdate(AudioDataSize); 
     try 
      BytesToRaw(AData, AudioData^, AudioDataSize); 
     finally 
      AudioBuffer.EndUpdate; 
     end; 
     end else 
     begin 
     Player.Active := False; 
     Player.WaitForStop; 
     end; 
    finally 
     LeaveCriticalSection(Section); 
    end; 
    except 
    end; 
end; 

我如何让他们在indy tcp以相同的方式工作?

回答

2

Indy的UDP和TCP组件使用不同的接口体系结构。仅移植代码是不够的,必须相应地重新设计通信协议,这可能需要您重新编写核心代码逻辑。请记住,UDP是基于消息的,但TCP不是,所以你必须设计自己的消息框架。

您还必须考虑到像TIdUDPServerTIdTCPServer也是多线程。但与TIdUDPServer不同,TIdTCPServer没有ThreadedEvent属性,因此您在访问其他线程(尤其是主UI线程)时必须提供自己的同步。

根据您的简单的例子,尝试这样的事情:

procedure TForm1.recorderData(Sender: TObject; const Buffer: Pointer; BufferSize: Cardinal; var FreeIt: Boolean); 
begin 
    FreeIt := True; 
    try 
    if IdTCPClient1.Connected then 
    begin 
     IdTCPClient1.IOHandler.Write(BufferSize); 
     IdTCPClient1.IOHandler.Write(RawToBytes(Buffer^, BufferSize)); 
     Exit; 
    end; 
    except 
    end; 
    stop.Caption := 'error'; 
end; 

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext); 
var 
    AudioDataSize: Integer; 
    AudioDataBytes: TIdBytes; 
    AudioData: Pointer; 
begin 
    AudioDataSize := AContext.Connection.IOHandler.ReadLongInt(); 
    AContext.Connection.IOHandler.ReadBytes(AudioDataBytes, AudioDataSize); 

    EnterCriticalSection(Section); 
    try 
    if AudioDataSize > 10 then 
    begin 
     try 
     if not Player.Active then 
     begin 
      Player.Active := True; 
      Player.WaitForStart; 
     end; 
     except 
     end; 
     if BlockAlign > 1 then Dec(AudioDataSize, AudioDataSize mod BlockAlign); 
     AudioData := AudioBuffer.BeginUpdate(AudioDataSize); 
     try 
     BytesToRaw(AudioDataBytes, AudioData^, AudioDataSize); 
     finally 
     AudioBuffer.EndUpdate; 
     end; 
    end else 
    begin 
     Player.Active := False; 
     Player.WaitForStop; 
    end; 
    finally 
    LeaveCriticalSection(Section); 
    end; 
end; 
+0

谢谢雷米这就是对我很有帮助。 – DelphiStudent 2014-09-10 20:01:36

+0

还有一件事雷米iam试图发送udp消息我正在这样做iDCLIENT.Broadcast(strMsg,16000);并读取像这样msg.Lines.Add(BytesToString(AData));如何在tcp中做到这一点? – DelphiStudent 2014-09-11 00:34:41

+0

TCP不支持广播。您必须将您的消息数据的单独副本发送给您拥有的每个单独的TCP连接。 – 2014-09-11 16:53:27