www.pudn.com > ChaoJiQiDong.rar > u_Chat.pas, change:2009-11-22,size:3403b


unit u_Chat; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ComCtrls, StdCtrls, Buttons, Menus, IdBaseComponent, 
  IdComponent, IdUDPBase, IdUDPClient, IdTCPConnection, IdTCPClient, 
  IdUDPServer,IdSocketHandle; 
 
type 
  TForm6 = class(TForm) 
    ListView1: TListView; 
    Memo1: TMemo; 
    PopupMenu1: TPopupMenu; 
    N1: TMenuItem; 
    N2: TMenuItem; 
    IdUDPClient1: TIdUDPClient; 
    IdUDPServer1: TIdUDPServer; 
    Edit1: TEdit; 
    Button1: TButton; 
    procedure FormCreate(Sender: TObject); 
    procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream; 
      ABinding: TIdSocketHandle); 
    procedure FormShow(Sender: TObject); 
    procedure N1Click(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
 
  end; 
var 
  Form6: TForm6=NIL; 
 
implementation 
uses u_ListView; 
{$R *.dfm} 
 
procedure TForm6.FormCreate(Sender: TObject); 
begin 
     ListView1.Columns[0].Width :=0; 
     idUDPServer1.DefaultPort :=5555; 
end; 
 
 
Function GetStr(S:String;Str1:String;Str2:String):String; 
var temp:string; 
begin 
  Result:=''; 
  If (Pos(Str1,S)=0) and (Pos(Str2,S)=0) Then Exit; 
  If (Str1='') and (Pos(Str2,S)<>0) then 
  begin 
    Result:=Copy(S,0,Pos(Str2,S)-1); 
    Exit; 
  end; 
  If (Pos(Str1,S)<>0) and (Str2='') then 
  begin 
    Result:=Copy(S,Pos(Str1,S)+Length(Str1),MaxInt); 
    Exit; 
  end; 
  temp:=Copy(S,Pos(Str1,S)+Length(Str1),MaxInt); 
  Result:=Copy(temp,0,Pos(Str2,temp)-1); 
end; 
 
function IPNameExists(IPStr: string): Boolean; 
var 
  i,ListCount:Integer; 
  str:string; 
begin 
  Result:=False; 
  ListCount:=Form6.ListView1.Items.Count; 
  if  ListCount=0 then exit; 
  Trim(IPStr); 
  for i:=0 to ListCount-1 do 
  begin 
      str:= Form6.ListView1.Items[i].SubItems.Strings[2]; 
      if str= IPStr then 
      begin 
        Result:=True; 
        Break; 
      end; 
  end; 
end; 
 
procedure TForm6.IdUDPServer1UDPRead(Sender: TObject; AData: TStream; 
  ABinding: TIdSocketHandle); 
var 
  Buf:array[0..255]of Char; 
  cmdStr,HeadStr,BodyStr:string; 
begin 
 FillChar(Buf,sizeof(Buf),#0); 
 AData.Read(Buf,sizeof(Buf)); 
 cmdStr:=Buf; 
 HeadStr:=Trim(GetStr(cmdStr,'',':')); 
 BodyStr:=Trim(GetStr(cmdStr,':','')); 
 if Form6.Visible then 
 begin 
     if HeadStr='BROAD' then 
     begin 
       if not IPNameExists(ABinding.PeerIP) then 
       begin 
          ListView_AddItems(ListView1,[InttoStr(ListView1.Items.Count+1),'',ABinding.PeerIP]); 
          IdUDPClient1.Send(ABinding.PeerIP,5555,'IP:'); 
       end; 
     end; 
 
     if HeadStr='IP' then 
     begin 
       if not IPNameExists(ABinding.PeerIP) then 
          ListView_AddItems(ListView1,[InttoStr(ListView1.Items.Count+1),'',ABinding.PeerIP]); 
     end; 
 end; 
 if HeadStr='chat' then 
 begin 
    if not  Form6.Visible then 
    begin 
       /// 
    end; 
    Memo1.Lines.Add(Format('%s ˵%s',[ABinding.PeerIP,BodyStr])); 
 end; 
end; 
 
procedure TForm6.FormShow(Sender: TObject); 
begin 
   ListView1.Items.Clear; 
   IdUDPServer1.Active :=True; 
end; 
 
procedure TForm6.N1Click(Sender: TObject); 
begin 
  ListView1.Items.Clear; 
  IdUDPClient1.Broadcast('BROAD:',5555); 
end; 
 
procedure TForm6.Button1Click(Sender: TObject); 
begin 
   IdUDPClient1.Broadcast(Format('chat:%s',[Edit1.Text]),5555); 
end; 
 
end.