自定义unit中,如何监视事件的发生? (150分)

  • 主题发起人 主题发起人 funner2
  • 开始时间 开始时间
F

funner2

Unregistered / Unconfirmed
GUEST, unregistred user!
我自己写了一个unit,要求能通过NMUDP发送并接受字符串
代码如下 @_@
发送(MyUDPSend)一切正常,可是接受部分(NMUDP1DataReceived)却无法工作,我将断点设
在NMUDP1DataReceived中,发现程序根本没有运行到该过程中。
如何解决呢? 谢谢

unit MyUDP;

interface

uses NMUDP,classes,Sysutils,Dialogs,forms;

type DDudp=class(tNMUDP)

private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner : TComponent);override;
class procedure MyUDPSend(RemoteHost:string;RemotePort:integer;StrtoSend:string);
class procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;FromIP: String; Port: Integer);
published
property OnDataReceived;
property LocalPort;
{ Published declarations }
end;

implementation

var
NMUDP1:TNMUDP;

constructor DDudp.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
LocalPort:=6767;
OnDataReceived :=NMUDP1DataReceived ;
end;


class procedure DDudp.MyUDPSend(RemoteHost:string;RemotePort:integer;StrtoSend:string);
var
MyStream: TMemoryStream;
TmpStr: String;
Begin
NMUDP1.RemoteHost := RemoteHost;
NMUDP1.RemotePort := RemotePort;
TmpStr := StrtoSend+inttostr(i);
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(tmpstr));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;

class procedure DDudp.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);
showmessage(tmpstr);
finally
MyStream.Free;
end;
end;


begin
NMUDP1:=TNMUDP.create(nil);



end.
 
LocalPort设的对吗?
 
同意楼上的,看看localport的设置
 
后退
顶部