2012-10-09 3 views
1

Embarcadero RAD Studio XE2 업데이트 4와 함께 제공된 Indy 패키지를 사용하고 있습니다.Delphi XE2 TIdUDPClient ReceiveString 오버로드가 작동하지 않습니다.

제 의도는 IP를 얻기 위해 서버로부터의 응답을 기다리는 TIdUDPClient로부터의 브로드 캐스트로 LAN에있는 서버를 찾는 것입니다. 인수없이 TIdUDPClient 메서드 ReceiveString 메서드를 사용하면 데이터를받을 수 있습니다.

RAD Studio와 함께 제공되는 10.5.8.3 버전의 Indy 10 Documentation 버전에서 오버로드 된 버전을 사용하려고하면 컴파일되지 않고 'E2250가 표시됩니다.'ReceiveString '의 오버로드 된 버전은 없습니다. 이 인수들로 불리는 '. succes에하지 않고, 거기에 설명 된대로 내가 인디의 online documentation는 IDE와 함께 제공되는 문서와 다른 것을 발견하고 그것을 시도

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. 

: 다음은 내 코드입니다.

도움이 될 것입니다.

답변

4

문제가 with 문에 의해 발생합니다, 당신은 ReceiveString 방법 대신에 지역 변수 portTIdUDPClientport 속성을 전달하고 있습니다. with 문을 사용하지 않는

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; 

또는 더 나은 : 해결 방법으로

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 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() 전화

, 첫 번째 오버로드를 호출 :

+1

명확하게'with' 문을 없애십시오 –

+0

+1 : 더 이상'with' –

+0

고마워요! 그게 많은 도움이됩니다! –

2

TIdUDPClientReceiveString() 2 과부하가 있습니다. 두 번째 오버로드를 호출 할 때 with 문이 로컬 port 변수 대신 TIdUDPClient.Port 속성을 두 번째 매개 변수로 전달하기 때문에 코드가 컴파일되지 않습니다. 컴파일을 통해 var 매개 변수에 속성을 전달할 수 없습니다.

충돌을 해결하려면 with 문을 제거하고 port 변수의 이름을 변경해야합니다.

+0

대단히 고마워요! –

관련 문제