【问题标题】:Delphi XE2 TIdUDPClient ReceiveString overload does not workDelphi XE2 TIdUDPClient ReceiveString 重载不起作用
【发布时间】:2012-10-09 00:21:58
【问题描述】:

我正在使用 Embarcadero RAD Studio XE2 Update 4 和随附的 Indy 包。

我的目的是在 LAN 中找到一个服务器,该服务器具有来自 TIdUDPClient 的广播,该客户端等待来自服务器的响应以获取其 IP。如果我使用不带参数的 TIdUDPClient 方法 ReceiveString 接收数据工作正常。

但是,当我尝试使用 RAD Studio 附带的 Indy 10 文档版本 10.5.8.3 中的重载版本时,它无法编译并显示 'E2250:没有重载版本的 'ReceiveString' 可以用这些参数调用'。 这是我的代码:

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.

我注意到 Indy 的 online documentation 与 IDE 附带的文档不同,并按照那里的描述进行了尝试,但没有成功。

任何帮助都会很棒!

【问题讨论】:

    标签: delphi compiler-errors delphi-xe2 broadcast indy10


    【解决方案1】:

    您的问题是由with 语句引起的,您将TIdUDPClientport 属性而不是局部变量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;
    

    作为解决方法,您可以像这样重命名 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;
    

    或者最好不要使用with 语句。

    【讨论】:

    • 绝对摆脱with声明
    【解决方案2】:

    TIdUDPClient 有 2 个 ReceiveString() 重载:

    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;
    

    当你在没有参数的情况下调用ReceiveString() 时,你调用的是第一个重载。尝试调用第二个重载时,您的代码无法编译,因为您的 with 语句将 TIdUDPClient.Port 属性传递给第二个参数,而不是您的本地 port 变量。编译将不允许您将属性传递给 var 参数。

    您需要删除 with 语句和/或重命名您的 port 变量以解决冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多