当前位置:首页 > 网络编程 > 软件语言 > DELPHI > 用Delphi编写局域网中的聊天程序

用Delphi编写局域网中的聊天程序

点击次数:43 次 发布日期:2008-11-09 08:39:13 作者:源代码网
源代码网推荐
广告载入中
Internet盛行的今天,网上聊天已成为一种时尚。同时,各单位已建成了自己的局域网;能否在局域网上实现聊天呢?可以,网上到处都有这种工具。当然,我们可以拥有自己版权的聊天工具。
源代码网推荐   User Datagram Protocol (UDP)协议,是一种无连接协议。在Delphi中利用这种协议,可以轻松的编写出聊天程序,以下的程序,在Delphi 5 Pwin98中通过。
源代码网推荐   打开Delphi,新建Application,放置以下几个控件:Panel1、Panel2,其属性如下:
源代码网推荐 FORM1.caption:=""聊天工具""
源代码网推荐 panel1.align:=albottom
源代码网推荐 panel2.align:=alclient
源代码网推荐   然后,放置以下控件:Edit1,ListBox1,Memo1,Button1,Button2,BitBtn1, Nmudp1其主要控件的属性如下:
源代码网推荐 nmudp1.localport:=8888(可自定义)
源代码网推荐 nmudp1.remoteport:=8888(与localport相同)
源代码网推荐 源程序如下:
源代码网推荐   unit main;
源代码网推荐   interface
源代码网推荐   uses
源代码网推荐   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
源代码网推荐   StdCtrls, Buttons, ExtCtrls, NMUDP, Menus, ComCtrls,WinSock; file://增加WinSock
源代码网推荐   type
源代码网推荐   TForm1 = class(TForm)
源代码网推荐   NMUDP1: TNMUDP;
源代码网推荐   Panel1: TPanel;

软件开发网 www.mscto.com


源代码网推荐   Panel2: TPanel;
源代码网推荐   Label1: TLabel;
源代码网推荐   Edit1: TEdit;
源代码网推荐   BitBtn1: TBitBtn;
源代码网推荐   Memo1: TMemo;
源代码网推荐   Panel3: TPanel;
源代码网推荐   Panel4: TPanel;
源代码网推荐   ListBox1: TListBox;
源代码网推荐   Button1: TButton;
源代码网推荐   Button2: TButton;
源代码网推荐   procedure FormShow(Sender: TObject);
源代码网推荐   procedure BitBtn1Click(Sender: TObject);
源代码网推荐   procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
源代码网推荐                  FromIP: String; Port: Integer);
源代码网推荐   procedure Edit1KeyPress(Sender: TObject; var Key: Char);
源代码网推荐   procedure Button1Click(Sender: TObject);
源代码网推荐   procedure Button2Click(Sender: TObject);
源代码网推荐   private
源代码网推荐    { Private declarations }
源代码网推荐   public
源代码网推荐    { Public declarations }
源代码网推荐   end;
源代码网推荐   var
源代码网推荐   Form1: TForm1;
源代码网推荐   ComputerName: array[0..127] of Char;
源代码网推荐   implementation
源代码网推荐   {$R *.DFM}
源代码网推荐   procedure TForm1.FormShow(Sender: TObject);
源代码网推荐   var
源代码网推荐    sz: dword;
源代码网推荐   begin
源代码网推荐    sz := SizeOf(Computername);
源代码网推荐    GetComputerName(ComputerName, sz);//得到本机的标识
源代码网推荐    ListBox1.Items.Clear;
源代码网推荐    ListBox1.Items.Add(""大家"");//在网友清单中,增加"大家"和
源代码网推荐    ListBox1.Items.Add(ComputerName);//本机名称
源代码网推荐    ListBox1.ItemIndex:=0;
源代码网推荐   end;
源代码网推荐   procedure TForm1.BitBtn1Click(Sender: TObject);
源代码网推荐   var
源代码网推荐    MyStream: TMemoryStream;
源代码网推荐    TmpStr: String;
源代码网推荐    i:integer;
源代码网推荐   Begin
源代码网推荐    if Edit1.Text<>"""" then file://如果所说的内容不为空,则发送。
源代码网推荐     begin
源代码网推荐      NMUDP1.ReportLevel := Status_Basic;
源代码网推荐      NMUDP1.RemotePort :=8888;//端口为:8888,可以自己定义,但必须与LocalPort相一致。
源代码网推荐      if ListBox1.Items[ListBox1.ItemIndex]=ComputerName then
源代码网推荐       Edit1.Text:=ComputerName ""自言自语道:"" Edit1.Text file://如果和自己对话.
源代码网推荐      Else
源代码网推荐       Edit1.Text:=ComputerName ""对"" ListBox1.Items[listbox1.itemindex] ""说:"" Edit1.Text;
源代码网推荐       TmpStr :=Edit1.text;
源代码网推荐       MyStream := TMemoryStream.Create;
源代码网推荐       try
源代码网推荐       MyStream.Write(TmpStr[1], Length(Edit1.Text));

源代码网推荐       if ListBox1.ItemIndex=0 then
源代码网推荐        begin
源代码网推荐         for i:=1 to ListBox1.Items.Count-1 do file://如果选择"大家",则对所有的网友发送信息
源代码网推荐           begin
源代码网推荐             NMUDP1.RemoteHost :=ListBox1.Items[i];//远程主机的名称或地址.
源代码网推荐             NMUDP1.SendStream(MyStream);//发送信息.
源代码网推荐         End;
源代码网推荐       end
源代码网推荐       else 如果私聊
源代码网推荐        begin
源代码网推荐         NMUDP1.RemoteHost :=ListBox1.Items[ListBox1.itemindex]; file://仅对所选中的网友.
源代码网推荐         NMUDP1.SendStream(MyStream);
源代码网推荐        End;
源代码网推荐       finally
源代码网推荐       MyStream.Free;
源代码网推荐      end;
源代码网推荐      Edit1.Text:="""";
源代码网推荐      Edit1.SetFocus;
源代码网推荐     end else
源代码网推荐     Edit1.SetFocus;
源代码网推荐     end;
源代码网推荐 procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
源代码网推荐     NumberBytes: Integer; FromIP: String; Port: Integer);
源代码网推荐     var
源代码网推荐      MyStream: TMemoryStream;
源代码网推荐      TmpStr: String;

源代码网推荐     begin
源代码网推荐       MyStream := TMemoryStream.Create;
源代码网推荐       try
源代码网推荐       NMUDP1.ReadStream(MyStream);
源代码网推荐       SetLength(TmpStr,NumberBytes);
源代码网推荐       MyStream.Read(TmpStr[1],NumberBytes);
源代码网推荐       Memo1.Lines.Add(TmpStr); file://显示对话的内容.
源代码网推荐       finally
源代码网推荐       MyStream.Free;
源代码网推荐      end;
源代码网推荐     end;
源代码网推荐    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
源代码网推荐    var
源代码网推荐     MyStream: TMemoryStream;
源代码网推荐     TmpStr: String;
源代码网推荐     i:integer;
源代码网推荐    Begin
源代码网推荐     if (key=#13) and (Edit1.Text<>"""") then file://如果所说的内容不为空,且最后一个按键为"Enter",则发送。
源代码网推荐      begin
源代码网推荐       NMUDP1.ReportLevel := Status_Basic;
源代码网推荐       NMUDP1.RemotePort :=8888;
源代码网推荐       if ListBox1.Items[ListBox1.ItemIndex]=ComputerName then
源代码网推荐         Edit1.Text:=ComputerName ""自言自语道:"" Edit1.Text
源代码网推荐       else
源代码网推荐         Edit1.Text:=ComputerName ""对"" ListBox1.Items[listbox1.itemindex] ""说:"" Edit1.Text;

源代码网推荐         TmpStr :=Edit1.text;
源代码网推荐         MyStream := TMemoryStream.Create;
源代码网推荐         try
源代码网推荐          MyStream.Write(TmpStr[1], Length(Edit1.Text));
源代码网推荐          if ListBox1.ItemIndex=0 then
源代码网推荐           begin
源代码网推荐            for i:=1 to ListBox1.Items.Count-1 do
源代码网推荐             begin
源代码网推荐              NMUDP1.RemoteHost :=ListBox1.Items[i];
源代码网推荐              NMUDP1.SendStream(MyStream);
源代码网推荐             end;
源代码网推荐           end
源代码网推荐           else
源代码网推荐            begin
源代码网推荐             NMUDP1.RemoteHost :=ListBox1.Items[ListBox1.itemindex];
源代码网推荐             NMUDP1.SendStream(MyStream);
源代码网推荐            end;
源代码网推荐            finally
源代码网推荐            MyStream.Free;
源代码网推荐            end;
源代码网推荐            Edit1.Text:="""";
源代码网推荐            edit1.SetFocus;
源代码网推荐           end else
源代码网推荐            Edit1.SetFocus;
源代码网推荐           end;
源代码网推荐     procedure TForm1.Button1Click(Sender: TObject);
源代码网推荐     var
源代码网推荐      InputString:String;
源代码网推荐      begin file://增加网友,输入的可以是IP地址或计算机名称。
源代码网推荐      InputString:=InputBox(""增加人员"", ""IP地址或计算机名"", """");
源代码网推荐      if Inputstring<>"""" then ListBox1.Items.Add(Inputstring);
源代码网推荐       ListBox1.ItemIndex:=0;
源代码网推荐      end;
源代码网推荐   procedure TForm1.Button2Click(Sender: TObject);
源代码网推荐    begin file://删除当前选中的网友,但"大家"不能被删除.
源代码网推荐     if ListBox1.ItemIndex<>0 then ListBox1.Items.Delete(ListBox1.ItemIndex);
源代码网推荐    end;
源代码网推荐   end.
源代码网推荐   这样,一个简单的聊天工具就做好了,只要双方同时运行本程序,且将对方的计算机名称或IP地址加入到网友中即可实现实时聊天了,一个属于自己版权的聊天程序,就这样编写成功了。当然,你可以增加更多的内容,使程序更加的完善,以求更多的功能。
软件开发网 www.mscto.com


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华