[英]Delphi XE2 TIdUDPClient ReceiveString overload does not work
I am using Embarcadero RAD Studio XE2 Update 4 and the Indy package shipped with it. 我正在使用Embarcadero RAD Studio XE2 Update 4及其附带的Indy软件包。
My intention is to find a server in LAN with broadcast from a TIdUDPClient that waits for a response from the server to get its IP. 我的意图是在局域网中找到一个服务器,该服务器具有TIdUDPClient广播的广播,该服务器等待服务器的响应以获取其IP。 Receiving the data works fine if I use the TIdUDPClient method ReceiveString with no arguments. 如果我使用不带参数的TIdUDPClient方法ReceiveString,则接收数据的效果很好。
But when I try to use the overloaded version found in the Indy 10 Documentation version 10.5.8.3 coming with RAD Studio, it does not compile and shows 'E2250: There is no overloaded version of 'ReceiveString' that can be called with these arguments' . 但是,当我尝试使用RAD Studio随附的Indy 10文档版本10.5.8.3中的重载版本时,它不会编译并显示'E2250:没有可以使用这些参数调用的'ReceiveString'重载版本' 。 Here is my code: 这是我的代码:
unit Client;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient, Vcl.StdCtrls, IdGlobal;
type
TFormLC = class(TForm)
UDPClient: TIdUDPClient;
LServer: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
FormLC: TFormLC;
implementation
{$R *.dfm}
function findServer:string;
var ans, ip : string;
port: TIdPort;
begin
with FormLC.UDPClient do begin
Active := True;
BroadcastEnabled:=True;
Broadcast('ServerRequest', 1234);
ans := ReceiveString(ip, port);
Active := False;
end;
if SameText(ans, 'ServerAccept') then
result := ip
else
result := '';
end;
procedure TFormLC.Button1Click(Sender: TObject);
var ans:string;
begin
LServer.Caption := findServer;
end;
end.
I noticed that the online documentation of Indy differs from the documentation that comes with the IDE and tried it as described there, without succes. 我注意到Indy的在线文档不同于IDE随附的文档,并按此处所述尝试了该文档,但没有成功。
Any help would be great! 任何帮助将是巨大的!
Your issue is caused by the with
statement, you are passing the port
property of the TIdUDPClient
instead of the local variable port
to the ReceiveString
method. 您的问题是由with
语句引起的,您正在将TIdUDPClient
的port
属性而不是局部变量port
传递给ReceiveString
方法。
function findServer:string;
var ans, ip : string;
port: TIdPort;
begin
with FormLC.UDPClient do begin
....
ans := ReceiveString(ip, port);//here you are passing the port property
Active := False;
end;
....
end;
As workaround rename you port
local variable like so : 解决方法是,重命名port
本地变量,如下所示:
function findServer:string;
var ans, ip : string;
vport: TIdPort;
begin
with FormLC.UDPClient do begin
....
ans := ReceiveString(ip, vport);//now will work
Active := False;
end;
end;
or even better don't use the with
statement. 甚至最好不要使用with
语句。
TIdUDPClient
has 2 overloads for ReceiveString()
: TIdUDPClient
对于ReceiveString()
有2个重载:
function ReceiveString(const AMSec: Integer = IdTimeoutDefault; AByteEncoding: TIdTextEncoding = nil{$IFDEF STRING_IS_ANSI}; ADestEncoding: TIdTextEncoding = nil{$ENDIF}): string; overload;
function ReceiveString(var VPeerIP: string; var VPeerPort: TIdPort; const AMSec: Integer = IdTimeoutDefault; AByteEncoding: TIdTextEncoding = nil{$IFDEF STRING_IS_ANSI}; ADestEncoding: TIdTextEncoding = nil{$ENDIF}): string; overload;
When you call ReceiveString()
without parameters, you are calling the first overload. 当您调用不带参数的ReceiveString()
,您正在调用第一个重载。 When trying to call the second overload, your code fails to compile because your with
statement is passing the TIdUDPClient.Port
property to the second parameter, instead of your local port
variable. 尝试调用第二个重载时,代码将无法编译,因为with
语句将TIdUDPClient.Port
属性传递给了第二个参数,而不是本地port
变量。 The compile will not allow you to pass a property to a var
parameter. 编译将不允许您将属性传递给var
参数。
You need to remove the with
statement and/or rename your port
variable to resolve the conflict. 您需要删除with
语句和/或重命名port
变量以解决冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.