udp部分的代码
procedure Tform_main.idudpUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Dstream: TStringStream;
rs:string;
ts:Tstrings;
tfile:Tstrings;
filename:string;
T:TgetpcThread;
i:integer;
msg:string;
filestr:Tfilestream;
buf:Tfilebuf;
p:integer;
begin
udpbusy:=true;
Dstream := TStringStream.Create('');
try
ts:=Tstringlist.Create;
Dstream.CopyFrom(AData, AData.Size);
ts.Text:=dstream.DataString;
if (ts.Strings[0]='getpcinfo') then
begin
rs:=form_main.get_pc_ip(form_main.pc_name)+#13#10+form_main.user_name+#13#10+form_main.user_part+#13#10+form_main.pc_name+#13#10+form_chat.ac_setstatus.Caption;
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, rs[1], Length(rs));
end
else if (ts.Strings[0]='deleteuser') then //接受网络的删除用户消息
begin
if ABinding.PeerIP<>form_main.get_pc_ip(form_main.pc_name) then
form_chat.deleteuser(ABinding.PeerIP);
end
else if (ts.Strings[0]='useroff') then //接受网络的用户离开消息
begin
with form_chat.finduserbyip(ABinding.PeerIP) do
imageindex:=2;
end
else if (ts.Strings[0]='useron') then //接受网络的用户上线消息
begin
with form_chat.finduserbyip(ABinding.PeerIP) do
imageindex:=3;
end
else if (ts.Strings[0]='adduser') then //接受网络的用户添加消息
begin
//if ABinding.PeerIP<>form_main.get_pc_ip(form_main.pc_name) then
begin
t:=TgetpcThread.Create(true);
t.iplist:=Tstringlist.Create;
t.iplist.Add(ABinding.PeerIP);
t.reqadd:=false;
t.Resume;
end;
end
else if (ts.Strings[0]='adduser_login') then //接受网络的删除用户退出消息
begin
t:=TgetpcThread.Create(true);
t.iplist:=Tstringlist.Create;
t.iplist.Add(ABinding.PeerIP);
t.reqadd:=false;
t.Resume;
//if ABinding.PeerIP<>form_main.get_pc_ip(form_main.pc_name) then
//begin
form_chat.reqaddpc(ABinding.PeerIP,false);
//end;
end
else if (ts.Strings[0]='chat') then //接受网络的用户聊天消息
begin
//form_chat.getpcinfo(ABinding.PeerIP);
with form_chat.finduserbyip(ABinding.PeerIP) do
begin
if checked then
begin
msg:='
'+formatdatetime('yy-mm-dd hh:mm:ss',now)+' 接收消息 ';
msg:=msg+form_main.get_user_infostring(caption,subitems.Strings[0],subitems.Strings[1],ABinding.PeerIP)+#13#10;
subitems.Strings[4]:=inttostr(strtoint(subitems.Strings[4])+1);
msg:=msg+httpdecode(ts.Strings[1])+#13#10+#13#10;
tim_writemsg.Enabled:=true;
if not tim_flasicon.Enabled then
tim_flasicon.Enabled:=true;
form_main.writemsg(msg);
form_main.playsound('message');
rs:='ok';
form_main.responsestatus(ABinding.PeerIP);
end
else
begin
rs:='';
end;
end;
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, rs[1], Length(rs));
end
else if (ts.Strings[0]='chat_off') then //接受网络的用户离开时的消息
begin
with form_chat.finduserbyip(ABinding.PeerIP) do
begin
if checked then
begin
msg:='
'+formatdatetime('yy-mm-dd hh:mm:ss',now)+' 用户离开 ';
msg:=msg+form_main.get_user_infostring(caption,subitems.Strings[0],subitems.Strings[1],ABinding.PeerIP)+#13#10;
subitems.Strings[4]:=inttostr(strtoint(subitems.Strings[4])+1);
msg:=msg+httpdecode(ts.Strings[1])+#13#10+#13#10;
form_main.writemsg(msg);
if not tim_flasicon.Enabled then
tim_flasicon.Enabled:=true;
form_main.playsound('message');
rs:='ok';
end
else
begin
rs:='';
end;
end;
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, rs[1], Length(rs));
end
else if (ts.Strings[0]='getfile') then
begin
with form_chat.finduserbyip(ABinding.PeerIP) do
begin
rs:='';
if checked then
begin
msg:='
'+formatdatetime('yy-mm-dd hh:mm:ss',now)+' 文件接收 ';
msg:=msg+form_main.get_user_infostring(caption,subitems.Strings[0],subitems.Strings[1],ABinding.PeerIP)+#13#10;
subitems.Strings[4]:=inttostr(strtoint(subitems.Strings[4])+1);
msg:=msg+'
注意:来自网络的文件可能含有有害信息,请先对文件进行病毒检查再打开!'+#13#10+'接收时可直接点击“接收”,或在“接受”上点右键,选择“目标另存为”,或者使用下载工具下载。'+#13#10;
msg:=msg+'文件:'+#13#10;
tfile:=Tstringlist.Create;
tfile.Text:=httpdecode(ts.Strings[1]);
for i:=1 to tfile.Count do
begin
if tfile.Strings[i-1]<>'' then
begin
filename:=extractfilename(httpdecode(tfile.Strings[i-1]));
msg:=msg+'
'+filename+' )+']接收'+''+#13#10;
end;
end;
tfile.Destroy;
msg:=msg+#13#10;
form_main.writemsg(msg);
if not tim_flasicon.Enabled then
tim_flasicon.Enabled:=true;
form_main.playsound('message');
rs:='ok';
form_main.responsestatus(ABinding.PeerIP);
end;
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, rs[1], Length(rs));
end;
end
else if (ts.Strings[0]='file') then //文件访问
begin
//showmessage(ts.Strings[0]);
if fileexists(ts.Strings[1]) {and (pos(ts.Strings[1],alwdownfilelist.Text)<>0)} then
begin
//showmessage(ts.Strings[0]);
zeromemory(@buf,sizeof(buf));
p:=strtoint(ts.Strings[2]);
buf.pos:=p;
filestr:=Tfilestream.Create(ts.Strings[1],fmOpenRead);
buf.size:=filestr.Size;
filestr.Position:=(p-1)*sizeof(buf.buf);
filestr.Read(buf.buf,sizeof(buf.buf));
filestr.Destroy;
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, buf, sizeof(buf));
end;
end
else
begin
rs:='';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, rs[1], Length(rs));
end;
ts.Free;
except
end;
dstream.Destroy;
udpbusy:=false;
end;