-
Hello! Is there a way to natively specify a timeout or number of connection attempts for the client before the "No connection could be made because the target machine actively refused it" exception is thrown? If not I'm assuming I'd have to resort to something like this: public void Connect()
{
int timer = 2;
while (true)
{
try
{
VA.WriteToLog("attempting connection...", "gray");
client.Connect();
break;
}
catch (Exception ex)
{
if (ex.Message.Contains("No connection could be made because the target machine actively refused it") == true)
{
timer += 2;
if (timer > connectionTimeout)
{
VA.WriteToLog("Error: " + ex.Message, "red");
break;
}
}
else
{
VA.WriteToLog("Error: " + ex.Message, "red");
break;
}
}
}
} With a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @Exergist there is a parameter is You could wrap The exception thrown by |
Beta Was this translation helpful? Give feedback.
Hi @Exergist there is a parameter is
SimpleTcpClient.Settings
calledConnectTimeoutSeconds
which should accomplish the stated goal of having a connection timeout.You could wrap
client.Connect()
in afor
loop to specify the number of retries, like you have above, but instead of managing a variable for the total number of seconds, you could just manage a variable for the number of retries.The exception thrown by
Connect()
if it's unable to connect is aSocketException
- might be easier to justcatch (SocketException)
than to parse the message. It's unlikely to be thrown for much else.