Using Delphi to write LAN chat program

Source: Internet
Author: User

Internet is popular today, online chatting has become a fashion. At the same time, all units have built their own local area network, can chat on the LAN? OK, it's all over the Internet. Of course, we can have our own copyright chat tools.

User Datagram Protocol (UDP) protocol, is a connectionless protocol. The use of this Protocol in Delphi, you can easily write a chat program, the following procedures, in the Delphi 5+pwin98.

Open Delphi, create a new application, place the following controls: Panel1, Panel2, whose properties are as follows:

form1.caption:= Chat Tool

Panel1.align:=albottom

Panel2.align:=alclient

Then, place the following controls: Edit1,listbox1,memo1,button1,button2,bitbtn1, Nmudp1 the properties of its primary control are as follows:

nmudp1.localport:=8888 (customizable)

nmudp1.remoteport:=8888 (same as LocalPort)

The source program is as follows:

Unit main;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,
Stdctrls, Buttons, Extctrls, NMUDP, Menus, Comctrls,winsock; file://Increase 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);//Get the identity of the machine
ListBox1.Items.Clear;
LISTBOX1.ITEMS.ADD (everyone);//In the list of users, add "everyone" and
LISTBOX1.ITEMS.ADD (ComputerName);//native name
listbox1.itemindex:=0;
End
Procedure Tform1.bitbtn1click (Sender:tobject);
Var
Mystream:tmemorystream;
tmpstr:string;
I:integer;
Begin
If edit1.text<> then file://send if the said content is not empty.
Begin
NMUDP1. Reportlevel: = Status_basic;
NMUDP1. RemotePort: =8888;//Port is: 8888, you can define your own, but must be consistent with the localport.
If Listbox1.items[listbox1.itemindex]=computername Then
Edit1.text:=computername+ to himself: +edit1.text file://if you talk to yourself.
Else
Edit1.text:=computername+ to +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 file://send information to all users if you choose "Everyone"
Begin
NMUDP1. RemoteHost: =listbox1.items[i];//The name or address of the remote host.
NMUDP1. Sendstream (MyStream);//Send information.
End;
End
else if you have a private conversation
Begin
NMUDP1. RemoteHost: =listbox1.items[listbox1.itemindex]; file://only for the selected netizens.
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://Displays the contents of the dialog.
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://if the said content is not empty and the last key is "Enter", send.
Begin
NMUDP1. Reportlevel: = Status_basic;
NMUDP1. RemotePort: = 8888;
If Listbox1.items[listbox1.itemindex]=computername Then
Edit1.text:=computername+ to himself: +edit1.text
Else
Edit1.text:=computername+ to +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
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://Add users, you can enter the IP address or computer name.
Inputstring:=inputbox (Add personnel, IP address or computer name,);
If Inputstring<> then ListBox1.Items.Add (inputstring);
listbox1.itemindex:=0;
End
Procedure Tform1.button2click (Sender:tobject);
Begin file://Delete the currently selected user, but "everyone" can not be deleted.
If Listbox1.itemindex<>0 then ListBox1.Items.Delete (Listbox1.itemindex);
End

End.

In this way, a simple chat tool is done, as long as both sides run the program at the same time, and the other computer name or IP address to join the user can realize real-time chat, a copy of their own copyright chat program, so written successfully. Of course, you can add more content, make the program more perfect, in order to more functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.