I want that my sockets connects to an backuphost when the socket can't connect to the host, i tried this:

if prclient.Socket.connected = false then
begin
prclient.Active := false;
prclient.Port := PORT;
prclient.Host := HOST;
prclient.Active := true;
prclient.Open;
sleep(500);
if prclient.Socket.Connected = false then
begin
prclient.Active := false;
prclient.Host := BACKUPHOST;
prclient.Active := true;
prclient.Open;
end;
end;

But now he doesn't connect at all. Who knows a working script?

有帮助吗?

解决方案

If you are using the socket in blocking mode then both Active:=True and Open() (which you SHOULD NOT be using together!) will raise an exception if the connection fails:

prclient.Port := PORT;
prclient.Host := HOST;
try
  prclient.Open;
except
  prclient.Host := BACKUPHOST;
  prclient.Open;
end;

If you are using the socket in non-blocking mode, then no exception is raised (unless a socket API function fails), the conection is attempted in the background, and you will be notified of the final result via the OnConnect or OnError event depending on whether the connection succeeds or fails, respectively.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top