我无法将OnUDPRead事件链接到IdUDPServer。
代码如下:
uses
System.SysUtils, IdUDPServer, IdGlobal, IdSocketHandle, Classes;
var
udp: TIdUDPServer;
buffer: TIdBytes;
myBinding: TIdSocketHandle;
procedure UDPRead (Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
bytes_received: integer;
begin
bytes_received:= AData.size;
end;
begin
myBinding := udp.Bindings.Add;
myBinding.IP := '0.0.0.0';
myBinding.Port := 5100;
udp.OnUDPRead := UDPRead; // Error occurs here
udp.Active := True;
end.
我收到了错误消息:
[dcc32 Error] ACUR401_20170802_UDPServer.dpr(16): E2029 ';' expected but 'OF' found
[dcc32 Error] ACUR401_20170802_UDPServer.dpr(28): E2009 Incompatible types: 'method pointer and regular procedure'
我相信我的困难在于我无法将事件处理程序链接到该过程。
答案 0 :(得分:0)
您必须为OnUDPRead属性分配匹配的对象方法。您分配的是常规程序。做这样的事情:
type
TMyObject = class
public
procedure UDPRead(AThread: TIdUDPListenerThread; AData: TIdBytes;
ABinding: TIdSocketHandle);
end;
{ TMyObject }
procedure TMyObject.UDPRead(AThread: TIdUDPListenerThread; AData: TIdBytes;
ABinding: TIdSocketHandle);
var
Received: Integer;
begin
Received := Length(AData);
...
end;
var
MyObject: TMyObject;
MyServer: TIdUDPServer;
MyBinding: TIdSocketHandle;
begin
MyServer := TIdUDPServer.Create(nil);
try
MyBinding := MyServer.Bindings.Add;
MyBinding.IP := '0.0.0.0';
MyBinding.Port := 5100;
MyObject := TMyObject.Create;
try
MyServer.OnUDPRead := MyObject.UDPRead;
MyServer.Active := True;
finally
MyObject.Free;
end;
finally
MyServer.Free;
end;
end.