简体   繁体   中英

Tclientsocket if socket can't connect

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM