我最近在编写一个局域网聊天的软件,现在遇到一个问题:在客户端同时下线时,服务器只会处理一个客户端下线.麻烦大家帮我看一下问题出在哪?下面是部分代码:
if msg.command='offOnline' then
begin
athread.Synchronize(offonline);
for i:=0 to high(online) do //删除下线用户的联机节点
begin
if online[i].userName=msg.userName then
begin
online[i]:=online[high(online)];
setlength(online,high(online));
break;
end;
end;
rec.command:='deleOnline';
rec.userName:=msg.userName;
for i:=0 to high(online) do //发送下线用户的用户名
begin
online[i].AThread.Connection.WriteBuffer(rec,sizeof(rec));
end; end;procedure Tmain.offonline;
var
i:integer;
begin
for i:=0 to listview1.Items.Count-1 do
begin
if listview1.Items.Item[i].Caption=msg.userName then
begin
listview1.Items.Item[i].Delete;
break;
end;
end;
memo1.Lines.Add('用户 '+msg.userName+' 下线了');
end;
if msg.command='offOnline' then
begin
athread.Synchronize(offonline);
for i:=0 to high(online) do //删除下线用户的联机节点
begin
if online[i].userName=msg.userName then
begin
online[i]:=online[high(online)];
setlength(online,high(online));
break;
end;
end;
rec.command:='deleOnline';
rec.userName:=msg.userName;
for i:=0 to high(online) do //发送下线用户的用户名
begin
online[i].AThread.Connection.WriteBuffer(rec,sizeof(rec));
end; end;procedure Tmain.offonline;
var
i:integer;
begin
for i:=0 to listview1.Items.Count-1 do
begin
if listview1.Items.Item[i].Caption=msg.userName then
begin
listview1.Items.Item[i].Delete;
break;
end;
end;
memo1.Lines.Add('用户 '+msg.userName+' 下线了');
end;
解决方案 »
- 请帮忙分析一下错误
- stringgrid 将修改后的多行内容提交到数据库
- 如何实现MDI窗口的工具栏融合?
- 如果控制网页中的视频播放?
- 请问哪有delphi使用com 编程得资料啊?急!!!
- 如何用DBgrideh打印出表头的内容:表头,日期,公司名称等等???
- 我的Function要返回select * from MyTable的内容,请问返回的数据集是什么类型的变量?
- 不知道怎么回事啊。
- 李维的书与技术并不是DELPHI开发发烧友们想象的那么高!
- 数据库连接时总是有密码输入框提示,怎么取消它???
- 诚聘:接口开发工程师(Delphi)(北京)
- 大侠救命:如何把28828800000这样的整数转化为1971-12-1号?
说明一下:listview是用来显示上线客户端信息的,rec是服务器发给客户端的消息包,online是array of RrectiveData RrectiveData=record
command:string[10];
userName:string[20];
fromWho:string[20];
toWho:string[20];
side:integer;
color:Tcolor;
text:string[100];
AThread:TIdPeerThread;
RmessageData=record
command:string[10];
userName:string[20];
password:string[20];
IP:string[15];
port:string[10];
fromWho:string[20];
toWho:string[20];
side:integer;
color:Tcolor;
text:string[100];
begin
online[i]:=online[high(online)];
setlength(online,high(online));
break;
end;仔细一看这段代码蹊跷,下线的又不一定是最后一个用户,无辜的给你挤掉了...
不过我还有个问题:我如果将程序改为TThreadlist的listlock锁住每个线程,线程中是否就可以不用Synchronize()?换句话说线程listlock后是否就一定是安全的?