讨论:如何保障ServerSocket/ClientSocket通讯中数据不丢失?(300分)

  • 主题发起人 主题发起人 chimney
  • 开始时间 开始时间
第一是包的大小不宜太大
第二就是您可以对每次的数据编号,如果又数据丢失或者接受的顺序不对也可以通过编号知道
 
不要一收到数据就处理,要把收的数据放到一个自己的缓冲里,用另外的线程来处理,另外再加大接收缓冲就可以了。
 
大家讨论的好热闹呀
 
我有这样的问题,做点对点语音聊天,不压缩,采样8k,单声道,统计发送数据库是8k/秒,接收却在6-7k/秒波动,实际听到对方声音象是被压榨,一顿一顿的。在本机运行2个实例,一个发送一个接收,速度都是8k/秒。我用的是ADSL,512k/2M,不知道是不是对方的带宽问题,但对方也是ADSL。代码如下:
//发送数据
procedure TForm1.WaveInBufferReady(Sender: TObject; lpWaveHdr: PWaveHdr);
var
i:integer;
begin
if SerSocket.Socket.ActiveConnections>0 then begin
for i:=0 to SerSocket.Socket.ActiveConnections-1 do
try SerSocket.Socket.Connections.SendBuf(lpWaveHdr^.lpData^,lpWaveHdr^.dwBytesRecorded);
WaveIn.Tag := WaveIn.Tag + integer(lpWaveHdr^.dwBytesRecorded);
except end;
SBar1.Panels[3].Text :='Server Send';
end else begin
if CliSocket.Active then begin
try CliSocket.Socket.SendBuf(lpWaveHdr^.lpData^,lpWaveHdr^.dwBytesRecorded);
WaveIn.Tag := WaveIn.Tag + integer(lpWaveHdr^.dwBytesRecorded);
except end;
end;
SBar1.Panels[3].Text :='Client Send';
end;
end;

//接收数据
procedure TForm1.WaveOutBufferLoad(Sender: TObject; lpWaveHdr: PWaveHdr; var MoreBuffers: Boolean);
begin
lpWaveHdr^.dwBytesRecorded:=0;
if CliSocket.Active and (CliSocket.Socket.ReceiveLength>0) then begin
lpWaveHdr^.dwBytesRecorded:=CliSocket.Socket.ReceiveBuf(lpWaveHdr^.lpData^,lpWaveHdr^.dwBufferLength);
WaveOut.Tag := WaveOut.Tag + integer(lpWaveHdr^.dwBytesRecorded);
SBar1.Panels[4].Text :='Client Rece';
end else begin
if SerSocket.Socket.ActiveConnections>0 then
if (SerSocket.Socket.Connections[0].ReceiveLength>0) then
try
lpWaveHdr^.dwBytesRecorded:=SerSocket.Socket.Connections[0].ReceiveBuf(lpWaveHdr^.lpData^,lpWaveHdr^.dwBufferLength);
WaveOut.Tag := WaveOut.Tag + integer(lpWaveHdr^.dwBytesRecorded);
except end;
SBar1.Panels[4].Text :='Server Rece';
end;
if lpWaveHdr^.dwBytesRecorded<1 then
begin
MemStream.Position :=0;
MemStream.Read(lpWaveHdr^.lpData^,lpWaveHdr^.dwBufferLength);
lpWaveHdr^.dwBytesRecorded:=lpWaveHdr^.dwBufferLength;
end;
MoreBuffers:=true;
end;
 
后退
顶部