unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NMUDP, ScktComp;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure NMUDP1BufferInvalid(var handled: Boolean;
var Buff: array of Char; var length: Integer);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1InvalidHost(var handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
C: Array [1..4] of Char;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
C := 'Love';
{The RemoteHost property specifies the dotted IP address or host name of the
remote computer that is the target of the SendBuffer or SendStream methods.}
NMUDP1.RemoteHost := '10.145.7.237';
{The ReportLevel property specifies the level of detail in the status reporting
given by the OnStatus event.}
NMUDP1.ReportLevel := Status_Basic;
{The LocalPort property specifies a port number to listen for datagram packets
sent to it on.}
NMUDP1.LocalPort := 8888;
{The RemotePort property specifies the port on the remote host to send data to
using the SendStream and SendBuffer methods.}
NMUDP1.RemotePort := 8888;
{The SendBuffer method is used for sending a buffer of data stored in an array
of char to the remote host.}
NMUDP1.SendBuffer(C, 3);
end;
procedure TForm1.NMUDP1BufferInvalid(var handled: Boolean;
var Buff: array of Char; var length: Integer);
begin
//内存缓冲错误的时候报错误
ShowMessage('Buffer Invalid: Buffer contains no data');
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
{The OnDataReceived event is called when data is received from the remote host}
var
I: Integer;
begin
if NumberBytes <= 100 then //The NumberBytes parameter specifies the number of incoming bytes.
begin
{
Declaration
procedure ReadBuffer(var Buff: array of char; var
length: integer);
Description
The ReadBuffer method reads incoming UDP data into a Buffer.
Parameters:
The Buff parameter specifies the buffer to read the data into.
The length parameter is the size of the data to be read.
}
NMUDP1.ReadBuffer(C, I);
//The FromIP parameter specifies the IP address of the computer that sent the data.
//The Port property specifies which port the data was sent from.
Memo1.Lines.Add('**接收到' + C + ' **大小:' + IntToStr(I)+' bytes **来自于 '+FromIP+' **端口 '+IntToStr(Port));
end
else
Memo1.Lines.Add(IntToStr(I)+' bytes incoming, buffer too small');
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
{The OnDataSend event is called when UDP data has been sent successfully by
either the SendStream or SendBuffer method.}
Memo2.Lines.Add('数据已经发送!');
end;
procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
S: String;
begin
//如果romote出现错误,就让客户自己指定romote ip
S := NMUDP1.RemoteHost;
if InputQuery('Invalid host', 'Specify valid hostname: ', S) then
begin
NMUDP1.RemoteHost := S;
handled := TRUE;
end;
END;
end.