I am new in coding client/server specially in indy, what I'm trying to do with my TCPClient here is if the main server IP is offline it will try to connect to the other ip, Any idea how to do that ? I'm just guessing it would be like...
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;
But im getting an error: "Count not bind socket." or "Address is already in use".
Read http://www.delphigroups.info/2/7/199566.html about BoundIP.
You cannot make TCPClient into scrolling list of ips. If you could - there would be a array-like property where you could put 10 or 100 of addresses, not as little as only two.
What you really have to do - is actually try to connect to different address
Example:
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);