To recreate this example, you will need to create a new blank Delphi application.Place 2 TEdits, 2 TMemos, a TNMMsg, and a TNMMsgServ on the form.Set the Host property of NMMsg1 to 127.0.0.1 This is the IP address for localhost. If you later wish to test this example across computers, set the Host property to the IP address of the computer you wish to communicate with.Insert the following code into Edit1's OnKeyPress event:procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);begin if Key = #13 then begin NMMsg1.FromName := Edit2.Text; NMMsg1.PostIt(Edit1.Text); end; end;When the key pressed in Edit1 is #13 (a carriage return, the first character sent when you press ENTER), the FromName property of NMMsg1 is set to the value of Edit2.Text. The message entered in Edit1 is then sent to the remote host via the PostIt method.Insert the following code into NMMsg1's OnMessageSent event:procedure TForm1.NMMsg1MessageSent(Sender: TObject);begin Memo2.Lines.Add('Message Sent'); end;This is the event handler for the OnMessageSent event. This simple adds the words Message Sent to Memo2, which is serving as a sort of status window.Insert the following code into NMMsgServ1's OnMSG event:procedure TForm1.NMMSGServ1MSG(Sender: TComponent; const sFrom, sMsg: String); begin Memo2.Lines.Add('Incoming message from '+sFrom); Memo1.Lines.Add('['+sFrom+'] '+sMsg); end;This is the event handler for the TNMMsgServ OnMSG event. When a message comes in, this event adds a line to Memo2 stating who the message is from. Memo1 is actually updated to include the new message and who it is from.Running the Application: When the application is run, simply type your name into Edit2. Type your message into Edit1, and press ENTER when you are finished. The message will be displayed in Memo1.
if Key = #13 then
begin
NMMsg1.FromName := Edit2.Text;
NMMsg1.PostIt(Edit1.Text);
end;
end;When the key pressed in Edit1 is #13 (a carriage return, the first character sent when you press ENTER), the FromName property of NMMsg1 is set to the value of Edit2.Text. The message entered in Edit1 is then sent to the remote host via the PostIt method.Insert the following code into NMMsg1's OnMessageSent event:procedure TForm1.NMMsg1MessageSent(Sender: TObject);begin
Memo2.Lines.Add('Message Sent');
end;This is the event handler for the OnMessageSent event. This simple adds the words Message Sent to Memo2, which is serving as a sort of status window.Insert the following code into NMMsgServ1's OnMSG event:procedure TForm1.NMMSGServ1MSG(Sender: TComponent; const sFrom, sMsg: String);
begin
Memo2.Lines.Add('Incoming message from '+sFrom);
Memo1.Lines.Add('['+sFrom+'] '+sMsg);
end;This is the event handler for the TNMMsgServ OnMSG event. When a message comes in, this event adds a line to Memo2 stating who the message is from. Memo1 is actually updated to include the new message and who it is from.Running the Application:
When the application is run, simply type your name into Edit2. Type your message into Edit1, and press ENTER when you are finished. The message will be displayed in Memo1.
你在服务器上运行TNMMsgServ的程序,假设该机器IP为192.168.1.20
其它机器上运行TNMMsg的程序,host设为192.168.1.20不行吗???……不过我没试过,请楼主试试!