Delphi 印地10号广播电台和AData
我正在尝试通过indy udp组件进行文本聊天,下面是服务器和客户端的代码 udp客户端:Delphi 印地10号广播电台和AData,delphi,indy,Delphi,Indy,我正在尝试通过indy udp组件进行文本聊天,下面是服务器和客户端的代码 udp客户端: procedure TForm1.SendClick(Sender: TObject); begin sendtocl.Broadcast(usertype.Text, 12000); usertype.Clear; end; onread服务器: procedure TForm1.UDPReceiverUDPRead(AThread: TIdUDPListenerThread; const AData
procedure TForm1.SendClick(Sender: TObject);
begin
sendtocl.Broadcast(usertype.Text, 12000);
usertype.Clear;
end;
onread服务器:
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;
begin
chatboxmsg.Lines.Add(BytesToString(AData));
end;
end;
工作正常,但如果我将udp客户端用于发送缓冲区“发送音频”等其他用途,我会遇到问题。chatboxmsg.line
以任何方式显示音频缓冲区的淹没数据,以使服务器读取分离的Adata
?在udp中,每次发送(Broadcast()
,send()
,SendBuffer())
等)传输不同的数据报。对于接收到的每个数据报,都会触发OnUDPRead
事件AData
一次包含一个不同数据报的数据
因此,您有两个选择:
OnUDPRead
处理程序就可以读取标识符/头,并知道是将剩余数据放入ChatBoxMsg
还是将其传递给音响系统TIdUDPServer
对象同时监听多个端口(这就是它的绑定
集合的目的),在这种情况下,OnUDPServer
事件的ABinding
参数将告诉您在哪个端口上接收到AData
。或者,只需使用两个独立的TIdUDPServer
对象,每个对象监听不同的端口,并为每个对象分配不同的OnUDPRead
处理程序欢迎来到堆栈溢出。要表示问题已解决,请选择解决问题的答案。如果您自己找到了另一个解决方案,请将该解决方案发布在“答案”部分,然后选择它。不要删除你问题的内容;它使现有的答案看起来像胡说八道。