编程实现在预设端口被占用时,自动递加端口号,直到找到一个没有使用的端口。但是为什么一下代码不能实现这个功能!
ok是一boolean变量,
repeat
try
idudpserver1.Active:=true;
ok:=true;
except
  on EIdCouldNotBindSocket do
    begin
    inc(idudPServer1.DefaultPort);
    end;
end;
until ok;

解决方案 »

  1.   

    repeat
    try
    idudpserver1.Active:=true;
    ok:=true;
    except
      on EIdCouldNotBindSocket do
        begin
        inc(idudPServer1.DefaultPort);
        ok:=False;    <----问题是不是这里?
        end;
    end;
    until ok;
      

  2.   

    晕看错了。试试这样repeat
    try
    idudpserver1.Active:=true;
    ok:=true;
    except
      on EIdCouldNotBindSocket do
        begin
          IdUDPServer1.Active:=false;
          IdUDPServer1.Bindings.Clear;
          ASocketHandle:=IdUDPServer1.Bindings.Add;
          ASocketHandle.Port:=1234;   //《--这里自己处理加一    end;
    end;
    until ok;
    asockethandle是TIDsocketHandle的对象
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, StdCtrls,idsockethandle;type
      TForm1 = class(TForm)
        Button1: TButton;
        IdUDPServer1: TIdUDPServer;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var ok:boolean ;
    tempport:integer;
    asockethandle:tidsockethandle;
    begin
    tempport:=idudpserver1.DefaultPort;
    repeat
    try
    idudpserver1.Active:=true;
    ok:=true;
    except
      on EIdCouldNotBindSocket do
        begin
          IdUDPServer1.Active:=false;
          IdUDPServer1.Bindings.Clear;
          ASocketHandle:=IdUDPServer1.Bindings.Add;
          tempport:=tempport+1;
          ASocketHandle.Port:=tempport ;    end;
    end;
    until ok;
    showmessage(inttostr(idudpserver1.Binding.Port));
    end;end.
    供参考