F
fengfan
Unregistered / Unconfirmed
GUEST, unregistred user!
我用 idtcpclient来接收数据,可是数据丢失严重,每三个包就要丢失两个包.服务器是VC写的,特别是在服务器连续发消息的时候,基本上智能接到一个.我写了一个线程来接收数据.到底是那里不对啊.我把线程附上:
TClientHandleThread = class(TThread)
private
mess: mess;
protected
procedure Execute; override;
public
end;
procedure TClientHandleThread.Execute;
var
size:integer;
Buffer:array[0..InBuffer_Size] of byte;
messmess;
filter1180:boolean;
begin
while not Terminated do
begin
if not form1.TcpClient.Connected then
Terminate
else
try
size:=form1.TcpClient.ReadFromStack();
form1.TcpClient.ReadBuffer(Buffer, size);
mess:= (Pmess((@buffer[0])));
filter1180:=false; //是否过滤消息
if mess.m_nHeader=1234 then
begin
if mess.m_nMessageId=1180 then
begin
if form1.Checkfilter1180.Checked then
begin
filter1180:=true;
end;
end;
end;
if not filter1180 then
begin
form1.addstrtotextfile('接收到 消息');//写日志文件
form1.EdtReceive.Text:=inttostr(strtoint(form1.EdtReceive.Text)+1);
form1.addstrtotextfile('解析 消息');//写日志文件
end;
mess:=form1.AnalysisMessage(buffer);//解析消息结构
if mess<>nil then
begin
form1.OutPutMessage(mess);//写日志文件
if not filter1180 then
begin
form1.addstrtotextfile('消息解析完成');//写日志文件
form1.SendMessage(mess,sizeof(mess));//将消息转发
end;
end
else
begin
form1.addstrtotextfile('消息解析失败');
end;
except
end;
end;
end;
TClientHandleThread = class(TThread)
private
mess: mess;
protected
procedure Execute; override;
public
end;
procedure TClientHandleThread.Execute;
var
size:integer;
Buffer:array[0..InBuffer_Size] of byte;
messmess;
filter1180:boolean;
begin
while not Terminated do
begin
if not form1.TcpClient.Connected then
Terminate
else
try
size:=form1.TcpClient.ReadFromStack();
form1.TcpClient.ReadBuffer(Buffer, size);
mess:= (Pmess((@buffer[0])));
filter1180:=false; //是否过滤消息
if mess.m_nHeader=1234 then
begin
if mess.m_nMessageId=1180 then
begin
if form1.Checkfilter1180.Checked then
begin
filter1180:=true;
end;
end;
end;
if not filter1180 then
begin
form1.addstrtotextfile('接收到 消息');//写日志文件
form1.EdtReceive.Text:=inttostr(strtoint(form1.EdtReceive.Text)+1);
form1.addstrtotextfile('解析 消息');//写日志文件
end;
mess:=form1.AnalysisMessage(buffer);//解析消息结构
if mess<>nil then
begin
form1.OutPutMessage(mess);//写日志文件
if not filter1180 then
begin
form1.addstrtotextfile('消息解析完成');//写日志文件
form1.SendMessage(mess,sizeof(mess));//将消息转发
end;
end
else
begin
form1.addstrtotextfile('消息解析失败');
end;
except
end;
end;
end;