H
hgptm
Unregistered / Unconfirmed
GUEST, unregistred user!
如題,
TmyUDP 單元見下:
unit UDPUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NMUDP,DateUtils;
type
TUDPRecDataEvent = procedure(Recstr:string) of object;
TMyUDP = Class
private
FRecstr: string;
//接收緩存
FonUDPRecData : TUDPRecDataEvent;
procedure FUDPRecData(Recstr:string);
procedure onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
protected
FLocalUDPPort: integer;
FRemoteUDPPort: integer;
FRemoteUDPIP: string;
NMUDP: TNMUDP;
Procedure SetLocalUDPPort(APort: integer);
Procedure SetRemoteUDPPort(APort: integer);
Procedure SetRemoteUDPIP(AIP: string);
public
constructor Create;
destructor Destroy;
override;
property LocalUDPPort: Integer read FLocalUDPPort write SetLocalUDPPort;
property RemoteUDPPort: integer Read FRemoteUDPPort write SetRemoteUDPPort;
property RemoteUDPIP : String Read FRemoteUDPIP write SetRemoteUDPIP;
Published
Property onUDPRecData: TUDPRecDataEvent Read FonUDPRecData write FonUDPRecData;
end;
implementation
constructor TMyUDP.Create;
begin
inherited Create;
FRecstr:='';
NMUDP := TNMUDP.Create(nil);
NMUDP.ReportLevel :=1;
NMUDP.OnDataReceived := onReceiveData;
end;
destructor TMyUDP.Destroy;
begin
if NMUDP <> nil then
begin
FreeAndnil(NMUDP);
end;
inherited;
end;
//設置本地端口
Procedure TMyUDP.SetLocalUDPPort(APort: integer);
begin
//if APort = FLocalUDPPort then
exit;
FLocalUDPPort := APort;
NMUDP.LocalPort := FLocalUDPPort;
end;
//設置遠程端口
Procedure TMyUDP.SetRemoteUDPPort(APort: integer);
begin
//if APort = FRemoteUDPPort then
exit;
FRemoteUDPPort := APort;
NMUDP.RemotePort := FRemoteUDPPort;
end;
//設置遠程IP
Procedure TMyUDP.SetRemoteUDPIP(AIP: string);
begin
//if Uppercase(trim(AIP)) = Uppercase(trim(FRemoteUDPIP)) then
exit;
FRemoteUDPIP := Uppercase(trim(AIP));
NMUDP.RemoteHost := FRemoteUDPIP;
end;
procedure TMyUDP.onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
SS: string;
MyStream: TMemoryStream;
begin
if NumberBytes=-1 then
exit;
MyStream := TMemoryStream.Create;
try
(Sender as TNMUDP).ReadStream(MyStream);
//非:NMUDP.ReadStream(MyStream);
ss :='';
SetLength(SS, NumberBytes);
MyStream.Read(SS[1], NumberBytes);
FRecstr := SS;
Finally
MyStream.Free;
end;
FUDPRecData(FRecstr);
//通知控件使用者:開始處理
end;
//通知 控件使用者
procedure TMyUDP.FUDPRecData(Recstr:string);
begin
if Assigned(onUDPRecData) then
begin
try
onUDPRecData(Recstr);
Except on E: Exceptiondo
begin
//showmessage(e.Message);
end;
end;
end;
end;
end.
TmyUDP 單元見下:
unit UDPUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NMUDP,DateUtils;
type
TUDPRecDataEvent = procedure(Recstr:string) of object;
TMyUDP = Class
private
FRecstr: string;
//接收緩存
FonUDPRecData : TUDPRecDataEvent;
procedure FUDPRecData(Recstr:string);
procedure onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
protected
FLocalUDPPort: integer;
FRemoteUDPPort: integer;
FRemoteUDPIP: string;
NMUDP: TNMUDP;
Procedure SetLocalUDPPort(APort: integer);
Procedure SetRemoteUDPPort(APort: integer);
Procedure SetRemoteUDPIP(AIP: string);
public
constructor Create;
destructor Destroy;
override;
property LocalUDPPort: Integer read FLocalUDPPort write SetLocalUDPPort;
property RemoteUDPPort: integer Read FRemoteUDPPort write SetRemoteUDPPort;
property RemoteUDPIP : String Read FRemoteUDPIP write SetRemoteUDPIP;
Published
Property onUDPRecData: TUDPRecDataEvent Read FonUDPRecData write FonUDPRecData;
end;
implementation
constructor TMyUDP.Create;
begin
inherited Create;
FRecstr:='';
NMUDP := TNMUDP.Create(nil);
NMUDP.ReportLevel :=1;
NMUDP.OnDataReceived := onReceiveData;
end;
destructor TMyUDP.Destroy;
begin
if NMUDP <> nil then
begin
FreeAndnil(NMUDP);
end;
inherited;
end;
//設置本地端口
Procedure TMyUDP.SetLocalUDPPort(APort: integer);
begin
//if APort = FLocalUDPPort then
exit;
FLocalUDPPort := APort;
NMUDP.LocalPort := FLocalUDPPort;
end;
//設置遠程端口
Procedure TMyUDP.SetRemoteUDPPort(APort: integer);
begin
//if APort = FRemoteUDPPort then
exit;
FRemoteUDPPort := APort;
NMUDP.RemotePort := FRemoteUDPPort;
end;
//設置遠程IP
Procedure TMyUDP.SetRemoteUDPIP(AIP: string);
begin
//if Uppercase(trim(AIP)) = Uppercase(trim(FRemoteUDPIP)) then
exit;
FRemoteUDPIP := Uppercase(trim(AIP));
NMUDP.RemoteHost := FRemoteUDPIP;
end;
procedure TMyUDP.onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
SS: string;
MyStream: TMemoryStream;
begin
if NumberBytes=-1 then
exit;
MyStream := TMemoryStream.Create;
try
(Sender as TNMUDP).ReadStream(MyStream);
//非:NMUDP.ReadStream(MyStream);
ss :='';
SetLength(SS, NumberBytes);
MyStream.Read(SS[1], NumberBytes);
FRecstr := SS;
Finally
MyStream.Free;
end;
FUDPRecData(FRecstr);
//通知控件使用者:開始處理
end;
//通知 控件使用者
procedure TMyUDP.FUDPRecData(Recstr:string);
begin
if Assigned(onUDPRecData) then
begin
try
onUDPRecData(Recstr);
Except on E: Exceptiondo
begin
//showmessage(e.Message);
end;
end;
end;
end;
end.