我在OnExecute事件中每执行一次就给一个公共变量加一次,发现如果客户连接后不发送数据就是1797次,如果发送数据的话就是3000多次不等?是什么原因啊?代码如下:
(每次有新的连接时testID被清0,为了保证只由一个线程控制,我只连接了一个客户进行测试)
procedure TfrmMain.tcpServerExecute(AThread: TIdPeerThread);
var
client:TClientInfo;
pp:Integer;
Msg:string;
begin
try
client :=TClientinfo(AThread.Data);
except //线程存储了非法数据,关闭。
AThread.Connection.Disconnect;
Exit;
end;
//测试执行次数
testID := testID + 1;
label2.Caption := inttostr(testID); if not client.UserInfo.Login then
begin
client.UserInfo.Name := AThread.Connection.ReadLn('',-1);
client.UserInfo.Login := true;
ClientList.Update(client); //更新客户数据
memMsg.Lines.Add(client.UserInfo.Name+' 登录服务器');
UpdateLstMember;
end
else
begin
pp:=client.UserInfo.pos;
Msg := GetNextMsg(@pp);
client.UserInfo.pos := pp;
try
AThread.Connection.WriteLn(Msg);
except
memMsg.lines.Add('向 '+ client.UserInfo.Name +' 发送数据失败,关闭该客户');
try
AThread.Connection.Disconnect;
except
end;
end;
end;
end;
(每次有新的连接时testID被清0,为了保证只由一个线程控制,我只连接了一个客户进行测试)
procedure TfrmMain.tcpServerExecute(AThread: TIdPeerThread);
var
client:TClientInfo;
pp:Integer;
Msg:string;
begin
try
client :=TClientinfo(AThread.Data);
except //线程存储了非法数据,关闭。
AThread.Connection.Disconnect;
Exit;
end;
//测试执行次数
testID := testID + 1;
label2.Caption := inttostr(testID); if not client.UserInfo.Login then
begin
client.UserInfo.Name := AThread.Connection.ReadLn('',-1);
client.UserInfo.Login := true;
ClientList.Update(client); //更新客户数据
memMsg.Lines.Add(client.UserInfo.Name+' 登录服务器');
UpdateLstMember;
end
else
begin
pp:=client.UserInfo.pos;
Msg := GetNextMsg(@pp);
client.UserInfo.pos := pp;
try
AThread.Connection.WriteLn(Msg);
except
memMsg.lines.Add('向 '+ client.UserInfo.Name +' 发送数据失败,关闭该客户');
try
AThread.Connection.Disconnect;
except
end;
end;
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货