在一本书上找到的一个聊天工具,请尽可能详细的注解下。 谢谢各位了
服务器端:
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
var
newitem:TListItem;
ii,jj:Integer;
user,usercur:string;
begin
newitem:=ListView1.Items.Insert(ListView1.Items.Count);
newitem.Caption:='Client:'+IntToStr(ListView1.Items.Count);
newitem.SubItems.Add(Socket.RemoteHost);
newitem.SubItems.Add(Socket.RemoteAddress);
newitem.SubItems.Add(IntToStr(Socket.RemotePort));
//ListView1.AlphaSort();
ListView1.Items.Item[ListView1.Items.Count-1].Selected:=true;
Display('连接到主机:'+Socket.RemoteAddress+' 成功');
ii:=ListView1.ItemIndex;
Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
ListView1.Items[ii].Caption+'加入进来!');
user := listview1.Items[ii].Caption + '^' +
listview1.Items[ii].SubItems[0] + '^'
+ listview1.Items[ii].SubItems[1] + '^'
+ listview1.Items[ii].SubItems[2];
for jj := 0 to listview1.Items.Count - 1 do
begin
serversocket1.Socket.Connections[jj].SendText('增加用户@#!^' +
user + '^');
end;
for jj := 0 to listview1.Items.Count - 2 do
begin
user := listview1.Items[jj].Caption + '^' +
listview1.Items[jj].SubItems[0] + '^'
+ listview1.Items[jj].SubItems[1] + '^'
+ listview1.Items[jj].SubItems[2];
serversocket1.Socket.Connections[ii].SendText('增加用户@#!^' +
user + '^');
end;
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
ii,jj:integer;
User:String;
begin
for ii:=0 to ListView1.Items.Count-1 do begin
if ListView1.Items.Item[ii].SubItems.Strings[2]=IntToStr(Socket.RemotePort) then begin
for jj:=0 to ListView1.Items.Count -1 do begin
user:=ListView1.Items[ii].Caption+'^'+ListView1.Items[ii].subitems[0]+'^'+
ListView1.Items[ii].SubItems[1]+'^'+ListView1.Items[ii].subitems[2];
ServerSocket1.Socket.Connections[jj].SendText('删除用户@#!^'+User+'^');
end;
Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
ListView1.Items[ii].Caption+'走了!');
ListView1.Items.Item[ii].Delete();
Display(Socket.RemoteHost+'走了');
end;
end;
if ListView1.Items.Count=0 then begin
Display('获取信息......');
end;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
tmpstr,User:string;
ii,jj:integer;
ListItem:TListItem;
exist:bool;
begin
exist:=False;
tmpstr:=Socket.ReceiveText;
if getmaskstring(tmpstr,'^',1)='交谈@#!' then begin
for jj:=0 to ListView1.Items.Count -1 do begin
user:=getmaskstring(tmpstr,'^',2);
ServerSocket1.Socket.Connections[jj].SendText('交谈@#!^'+User+'^');
end;
Richedit1.Lines.Add(getmaskstring(tmpstr,'^',2));
end;
if getmaskstring(tmpstr,'^',1)='昵称@#!' then begin
for ii:=0 to ListView1.Items.Count-1 do begin
if Socket.RemotePort=StrToInt(ListView1.Items[ii].SubItems[2])
then begin
exist:=true;
if getmaskstring(tmpstr,'^',2)<>ListView1.Items[ii].Caption then
begin
Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
ListView1.Items[ii].Caption+' 更名为 '+
getmaskstring(tmpstr,'^',2));
ListView1.Items[ii].Caption:=getmaskstring(tmpstr,'^',2);
for jj:=0 to ListView1.Items.Count -1 do begin
user:=ListView1.Items[ii].Caption+'^'+ListView1.Items[ii].subitems[0]+'^'+
ListView1.Items[ii].SubItems[1]+'^'+ListView1.Items[ii].subitems[2];
ServerSocket1.Socket.Connections[jj].SendText('昵称@#!^'+User+'^');
end;
end;
end;
end;
end;
end;
客户端:
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
tmpstr:string;
newItem:TListItem;
ii:Integer;
exist:bool;
commHead:string;
begin
exist:=false;
tmpstr:=Socket.ReceiveText;
commHead:=getmaskstring(tmpstr,'^',1);
//Richedit1.Lines.Add(tmpstr);
if commHead ='昵称@#!'then
begin
for ii:=0 to ListView1.Items.Count -1 do
begin
if getmaskstring(tmpstr,'^',5)=ListView1.Items[ii].SubItems[2]
then
begin
exist:=true;
if getmaskstring(tmpstr,'^',2)<>ListView1.Items[ii].Caption then
begin
Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
ListView1.Items[ii].Caption+' 更名为 '+
getmaskstring(tmpstr,'^',2));
ListView1.Items[ii].Caption:=getmaskstring(tmpstr,'^',2);
end;
end;
end;
end;
if commHead='交谈@#!'then
begin
Richedit1.Lines.Add(getmaskstring(tmpstr,'^',2));
end;
if commHead='增加用户@#!'then
begin
for ii:=0 to ListView1.Items.Count-1 do
begin
if getmaskstring(tmpstr,'^',5)=ListView1.Items[ii].SubItems[2] then
exist:=true;
end;
if exist=false then begin
newitem:=ListView1.Items.add();
newitem.Caption:=getmaskstring(tmpstr,'^',2);
newitem.SubItems.Add(getmaskstring(tmpstr,'^',3));
newitem.SubItems.Add(getmaskstring(tmpstr,'^',4));
newitem.SubItems.Add(getmaskstring(tmpstr,'^',5));
Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
getmaskstring(tmpstr,'^',2)+'加入进来!');
end;
end;
if commHead='删除用户@#!'then
begin
for ii:=0 to ListView1.Items.Count-1 do
begin
if getmaskstring(tmpstr,'^',5)=ListView1.Items[ii].SubItems[2] then
begin
Listview1.Items.Delete(ii);
Richedit1.Lines.Add(DateToStr(Date)+' '+TimeToStr(time)+'==>>'+
getmaskstring(tmpstr,'^',2)+'走了!');
end;
end;
end;
end;