定义一个包格式
Type
TBufferType=Array [1..1] of Byte;
PBufferType=^TBufferType;
DataBuf=Record
Size:LongWord;
Data:TBufferType;
End;
发送:
procedure SendQueue.SendAudio;
Var
s:LongWord;
p1:^DataBuf;
Temp:Array[1..5*1024] of Byte;
str:String;
begin
If NOT Terminated Then
Begin
If FAudioQueue.Count>0 Then
Begin
p1:=FAudioQueue.Pop;
Try
If MainForm.IdTCPClient.Connected Then
Begin
s:=p1^.Size+4+4;
System.Str(s,str);
FillChar(Temp,4,0);
Move(p1^,Temp[5],p1^.Size+4);
MainForm.IdTCPClient.WriteLn('D'+str);
MainForm.IdTCPClient.WriteBuffer(Temp,s,True);
End;
Finally
FreeMem(p1);
End;
End;
End;
end;
接受:
procedure TMainForm.IdTCPServerExecute(AThread: TIdPeerThread);
Var
s:String;
ds,s1,s2:LongWord;
code:Integer;
temp:array[1..20*1024] of Byte;
p,p1:^DataBuf;
begin
s:=AThread.Connection.ReadLn;
If s[1]<>'D' Then Exit;
Delete(s,1,1);
Val(s,ds,code);
AThread.Connection.ReadBuffer(temp,ds);
Move(temp,s1,4);
If s1<>0 Then
Begin
GetMem(p,s1+4);
Move(Temp,p^,s1+4);
CodedFrame1.Push(p);
VDC.Execute;
End;
Move(Temp[4+s1+1],s2,4);
GetMem(p1,s2+4);
Move(Temp[4+s1+1],p1^,s2+4);
OriAudio1.Push(p1);
end;