9swampy / Telnet

Published on Nuget at https://www.nuget.org/packages/Telnet

Home Page:http://www.nugetmusthaves.com/Package/Telnet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TryLoginAsync attempt 2

GromitD90 opened this issue · comments

Sorry about messing up that last post, as well as accidentally closing it ! Here it is again:

I have a program I wrote that logs in to a Netgear R7000 wireless access point using Prims Telnet. By default the R7000 does not support Telnet but there is a way of enabling it. When enabled my program works without issue. I log in using the TryLoginAsync method, issue the command I want using WriteLine and getting the response to parse using TerminatedReadAsync. I log out using the Dispose method. This sequence is repeated every couple of minutes and has run for weeks without any problems.
My issue is when Telnet is not enabled on the R7000 which can happen if the router has to be rebooted. When this happens my program just hangs on the TryLoginAsync call and will sit there until Telnet is enabled.

I thought that by setting a timeout on the call I would be able to detect this but so far have been unable to get it to work. Here is the code I'm using:
var telnetClient = new PrimS.Telnet.Client(IPaddress, 23, new CancellationToken());
var connection = await telnetClient.TryLoginAsync("root", "rovers15", 20000);
if (telnetClient.IsConnected)
{
do my thing
}
else
{
Handle cannot connect event
}

I've tried a few variations on the above but the else condition is never triggered.

Can anyone tell me what I am doing wrong?
TIA
Mike

Look at the code inside TryLoginAsync; it's just a wrapper for a few consecutive reads/writes. The CancellationToken/Timeout implementation I put in but used minimally and it served my purposes; clearly you've found something that isn't quite right.

After further digging I have found that it is not the TryLoginAsynch call that is the issue. It is the call to create a new telnetclient object.
If Telnet is not enabled on my R7000 the TCP session request is refused which I assume is sent by the call -
var telnetClient = new PrimS.Telnet.Client(IPaddress, 23, new CancellationToken());

So I assume the telnetClient object will not get created but I'm not savvy enough to understand why I don't get an exception in the subsequent code.

I have found a workaround. If I call the async method containing the code above with an await inside a try catch then I do catch an exception. so the following works:

public static async Task CheckTelnetCanConnect()
{
var telnetClient = new PrimS.Telnet.Client("XXX.XXX.XX.XX", 23, new CancellationToken());
var connection = await telnetClient.TryLoginAsync("root", "password", loginTimeoutMs: 10000);
if (telnetClient.IsConnected)
{
Console.WriteLine("Telnet is Connected");
Console.ReadLine();
telnetClient.Dispose();
return true;
}
else
{
Console.WriteLine("Telnet Connect Failed");
Console.ReadLine();
telnetClient.Dispose();
return false;
}
}

Called by -

try
{
await CheckTelnetCanConnect();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
I get a Socket exception telling me the target machine actively refused the connection (TCP SYN) request.

If the await is not used on the call I see no exceptions or errors anywhere

Mike

Thanks for coming back and documenting. You've got something that works now so I'll close.