【问题标题】:TCPClient connecting to ip listTCPClient 连接到 ip 列表
【发布时间】:2014-01-16 12:50:00
【问题描述】:

我是 indy 编码客户端/服务器的新手,我在这里尝试使用我的 TCPClient 做的是,如果主服务器 IP 处于脱机状态,它将尝试连接到另一个 IP,知道怎么做?我只是猜测它会像......

  IdTCPClient1.ReuseSocket := rsTrue;
  IdTCPClient1.BoundIP :=   '192.168.1.31'; //other ip here
  IdTCPClient1.BoundPort := 5000;
  IdTCPClient1.Host := 'localhost';//main ip server here
  IdTCPClient1.Port := 5000;

  IdTCPClient1.Connect;

但我得到一个错误:“计数未绑定套接字。”或“地址已被使用”。

【问题讨论】:

  • 一般建议,如果你没有真正海量和频繁的数据交换,比如洪流:几十 GB 拆分成几百万个孤立的短数据包,最好使用 HTTP + JSON。它是文本的和标准的 = 有很多工具可以调试任何问题,包括您自己的眼睛,防火墙没有问题,以及 Delphi 的六个库,您可以出于任何原因从“不工作”到“不工作”中进行选择喜欢”
  • 我会不理会BoundPort (0),让操作系统选择一个空闲端口号。

标签: delphi delphi-7 indy indy10


【解决方案1】:

阅读http://www.delphigroups.info/2/7/199566.html 了解BoundIP。

你不能让 TCPClient 进入 ips 的滚动列表。如果可以的话 - 会有一个类似数组的属性,您可以在其中放置 10 或 100 个地址,而不是只有两个。

你真正要做的——实际上是尝试连接到不同的地址

例子:

function TryToConnect(const server: string): boolean; overload;
begin
  try
    IdTCPClient1.ReuseSocket := rsTrue;
    IdTCPClient1.Host := server;
    IdTCPClient1.Port := 5000;

    IdTCPClient1.Connect;
    Result := true;
  except on E: Exception do begin
    Result := false;
    LogToFileAndScreenAnError(E.ClassName + ' ==> ' + E.Message);
  end;
end;

function TryToConnect(const servers: array of string): boolean; overload;
var i: integer;
begin
  Result := false;
  for i := Low(servers) to High(Servers) do begin
      Result := TryToConnect( servers[i] );
      if Result then break;
  end;


  if not Result then LogAndShowError('Could not connect to any of servers! Read log file!');

 { or maybe even

  if not Result then raise Exception.Create('Could not connect to any of servers! Read log file!');

 }
end;


var ss: TStringDynArray;

SetLength(SS, 2);
ss[0] := 'localhost';
ss[1] := '192.168.3.10';

Success := TryToConnect(ss);

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 2015-08-16
    • 2012-11-12
    • 2011-09-10
    • 2016-04-01
    • 2016-04-08
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多