Write a UDP chat program in a local area network with Delphi (Favorites)

zhaozj2021-02-12  140

Write the UDP chat program in the LAN with Delphi today, online chat has become a fashion. At the same time, all units have built their own local area network; can you chat on the local area network? Yes, this tool is everywhere. Of course, we can have a chat tool with your own copyright. User DataGram Protocol (UDP) protocol is a connectionless protocol. Using this protocol in Delphi, you can easily write a chat program, the following programs are passed in Delphi 5 PWIN98.

Open Delphi, New Application Place the following controls: Panel1, Panel2, its properties are as follows: then place the following control: Edit1ListBOX1MEMO1BUTTON1BUTTON2BITBTN1BUTTON1BUTTON2BITBTN1NMUDP1 The properties of its main controls are as follows: The functions of each major controls are as follows: The current interface is as follows: The source program is as follows: Unit main; interfaceusesWindows, messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, NMUDP, Menus, ComCtrls, WinSock; file: // increase WinSock typeTForm1 = 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; varForm1: TForm1; ComputerName: array [0..127] of Char; implementation {$ R * .DFM} procedure TForm1.FormSh OW (sender: TOBJECT); VARSZ: DWORD; begins: = sizeof (computername); getComputerName (ComputerNamesz); // Get the identity of this unit ListBox1.Items.clear; ListBox1.Items.Add ('everyone'); // In the netizen list, add "everyone" and listbox1.items.add (computername); // Name listbox1.itemindex: = 0; end; procedure tform1.bitbtn1click (sender: TOBJECT); VarmyStream: TMemoryStream; Tmpstr: String i: integer; beginif edit1.text <> '' Then File: // If the content is not sent for flight. Begin nmudp1.reportlevel: = status_basic; nmudp1.remoteport: = 8888; // port is: 8888, you can define it yourself, but you must match LocalPort.

If listbox1.items [listbox1.itemindex] = computername kiln Edit1.text: = computername 'Self-like:' Edit1.text File: // If you talk to your own, Else Edit1.text: = computername 'pair' Listbox1.items [listbox1.itemindex] 'said:' 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 file: // If you select "Everyone", send information nmudp1.remotehost: = listbox1.items [i ]; // Remote host name or address. Nmudp1.sendStream (MyStream); // Send information. End; Else Begin If private chat nmudp1.remotehost: = ListBox1.items [listbox1.itemindex]; file: // only of the selected users NMUDP1.SendStream (MyStream); End; finally MyStream.Free; end; Edit1.Text:. = ''; Edit1.SetFocus; end elseEdit1.SetFocus; end; procedure TForm1.NMUDP1DataReceived (Sender: TComponent; Numberbytes: Integer; fromip: string; port: integer; tmemorystream; tmpstr: string; beginmystream: = tmemorystream.create; try nmud P1.readstream (MyStream); setlength; MyStream.read (Tmpstr [1] Numberbytes); Memo1.Lines.Add (Tmpstr); file: // Displays the content of the conversation. Final or MyStream.free; end; end; Procedure TForm 1.EDit1KeyPress (Sender: Tobject; Var Key: Char); VarmyStream; TMemoryStream; TMPSTR: STRING; I: Integer; Beginif (key = # 13) and (edit1.text <> ') THEN FILE: // If The content is not empty, and the last button is "Enter".

转载请注明原文地址:https://www.9cbs.com/read-7083.html

New Post(0)