用Delphi編寫區域網路聊天程式

來源:互聯網
上載者:User

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;
   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地址加入到網友中即可實現即時聊天了,一個屬於自己著作權的聊天程式,就這樣編寫成功了。當然,你可以增加更多的內容,使程式更加的完善,以求更多的功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.