Delphi TCP客户端正在连接到ip列表
我对客户机/服务器编码是新手,特别是在indy,我想用我的TCPClient做的是,如果主服务器IP脱机,它会尝试连接到另一个IP,知道怎么做吗?我只是猜测它会像Delphi TCP客户端正在连接到ip列表,delphi,delphi-7,indy,indy10,Delphi,Delphi 7,Indy,Indy10,我对客户机/服务器编码是新手,特别是在indy,我想用我的TCPClient做的是,如果主服务器IP脱机,它会尝试连接到另一个IP,知道怎么做吗?我只是猜测它会像 IdTCPClient1.ReuseSocket := rsTrue; IdTCPClient1.BoundIP := '192.168.1.31'; //other ip here IdTCPClient1.BoundPort := 5000; IdTCPClient1.Host := 'localhost';/
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;
但我得到一个错误:“计数不绑定套接字。”或“地址已在使用” 阅读关于BoundIP的内容
您不能将TCPClient设置为IP的滚动列表。如果可以的话,将有一个类似于数组的属性,可以放置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);
一般建议,如果您没有真正大规模和频繁的数据交换,比如torrents:将几十GB的数据拆分为数百万个短的孤立数据包,那么最好使用HTTP+JSON。它是文本和标准的=有很多工具可以调试任何问题,包括你自己的眼睛,防火墙和六个Delphi库没有问题,你可以从“不工作”到“不喜欢”中选择,我会让
BoundPort
单独使用(0
),让操作系统选择一个自由端口号。