2012-10-09 37 views
1

我正在使用Embarcadero RAD Studio XE2 Update 4以及随附的Indy软件包。Delphi XE2 TIdUDPClient ReceiveString过载不起作用

我的意图是在局域网中找到一台服务器,并使用来自TIdUDPClient的广播,等待服务器获取其IP的响应。如果我使用没有参数的TIdUDPClient方法ReceiveString,接收数据就可以正常工作。

但是当我尝试使用RAD Studio附带的Indy 10 Documentation版本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. 

我注意到,印的online documentation从自带的IDE的文档不同,试了一下在那里所述,无更迭。

任何帮助将是伟大的!

回答

4

你的问题是由with声明引起的,要传递的TIdUDPClient,而不是局部变量portport属性的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声明。

+1

肯定摆脱'with with'语句 –

+0

+1:没有更多'with' –

+0

谢谢!这有很大帮助! –

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变量以解决冲突。

+0

非常感谢! –