【问题标题】:Adjust TCP/UDP Server Runtime调整 TCP/UDP 服务器运行时间
【发布时间】:2016-11-18 20:27:18
【问题描述】:

我正在使用 Embarcadero RAD Studio 10。我正在尝试在我的应用程序中使用 Indy 客户端/服务器组件。

我想在运行时调整 TCP/UDP 服务器 IP 地址和端口。

我可以在设计时看到默认设置:

我可以向Bindings 添加条目并设置DefaultPort

但是,我想在程序运行时执行此操作。我想在我的 UI 中设置绑定和端口,然后按下一个按钮让服务器使用我输入的内容。

我该怎么做?

【问题讨论】:

    标签: tcp server udp client indy


    【解决方案1】:

    BindingsTIdSocketHandle 对象的集合。在设计时向集合添加新条目与在运行时调用 Bindings.Add() 方法相同。

    TIdSocketHandle 具有 IPPort 属性。当一个TIdSocketHandle 对象被创建时,它的Port 被初始化为DefaultPort 的当前值。

    要执行您的要求,只需调用Bindings.Add() 并设置新对象的IPPort 属性。例如:

    德尔福:

    procedure TMyForm.ConnectButtonClick(Sender: TObject);
    var
      LIP: string;
      LPort: TIdPort;
      LBinding: TIdSocketHandle;
    begin
      LIP := ServerIPEdit.Text;
      LPort := IntToStr(ServerPortEdit.Text);
      IdTCPServer1.Active := False;
      IdTCPServer1.Bindings.Clear;
      LBinding := IdTCPServer1.Bindings.Add;
      LBinding.IP := LIP;
      LBinding.Port := LPort;
      IdTCPServer1.Active := True;
    end;
    

    C++:

    void __fastcall TMyForm::ConnectButtonClick(TObject *Sender);
    {
        String LIP = ServerIPEdit->Text;
        TIdPort LPort = IntToStr(ServerPortEdit->Text);
        IdTCPServer1->Active = false;
        IdTCPServer1->Bindings->Clear();
        TIdSocketHandle *LBinding = IdTCPServer1->Bindings->Add();
        LBinding->IP = LIP;
        LBinding->Port = LPort;
        IdTCPServer1->Active = true;
    }
    

    TIdUDPServer 相同。

    【讨论】:

    • 这段代码是 delphi 但我需要 C++ 你能用 C++ 做这个吗?
    • @Buraki 您的问题中没有 nothing 表明您使用的是 C++ vs Delphi。下次请提供这些细节,它们很重要。
    • 对不起,这是我的错,下次我会小心的。
    猜你喜欢
    • 2023-03-28
    • 2011-01-19
    • 2012-01-04
    • 1970-01-01
    • 2020-04-08
    • 2015-11-24
    • 1970-01-01
    • 2012-06-19
    • 2012-12-18
    相关资源
    最近更新 更多