Click here to Skip to main content
14,387,650 members
Rate this:
Please Sign up or sign in to vote.
Hi guys, any idea on how am I suppose to create an application on C# 5.0 using async await Telnet client.

My scenario is that I have to telnet multiple servers at the same time. Also, i need it to check the servers continuously just like a ping /t function.

Thanks much.. :)
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Why?! Create a separate thread to work with each separate telnet server and work synchronously. Synchronize those threads using thread synchronization in those rare cases when it is needed.

I think asynchronous APIs get popularity when threads were not a commonplace, or just because many people purely understood threading. After all, asynchronous operations are often also based on threads, only under the hood. Using threads explicitly is way more straightforward and give more stable results, because 1) you have full control, 2) for synchronization, you use the same thread synchronization techniques over and over, as they are universal, not application-specific, 3) in contrast to that asynchronous APIs are application-specific, 4) asynchronous APIs just use more complex logic of their use, which is less obvious.

—SA
   
Comments
Matthew Dennis 15-Jul-14 13:41pm
   
You obviously don't understand how Async works. When an async method awaits a Task, the Thread is made available for use for the execution of other Tasks. This means that each Thread will execute multiple Tasks and will scale better. If you just spin up a new Thread for each chunk of work you will end up creating and time-slicing a large number of threads, each of which has an overhead in terms of memory and CPU.
This is shown in MS examples that have a web site using 5000 Threads for a certain level of concurrent user access, while the async version of the same site will only need 50 Threads and a lot less memory/CPU making the system much more scalable.
I personally find the async/await code much easier to write and read, but it does take a certain level of study and experimentation until you reach the 'Eureka' moment and it all makes sense.
Sergey Alexandrovich Kryukov 15-Jul-14 13:45pm
   
I know. Still, it does not seem convincing to me. Relying on threads does not mean using many threads. You probably assume that I would suggest something stupid, like a separate thread per channel (as many trying to design so, quite foolishly). Is so, you assume something which I never meant. I could claim that you don't understand my approach on the same grounds as you claimed that I don't understand async. operation. But I did not claim that even now, because I have no clear facts to support this idea.

So, I still keep my believe that thread approach, when used wisely, is usually more practical. This is based on my own Eureka moments.

—SA
Matthew Dennis 15-Jul-14 13:56pm
   
Despite the fact that the async/await pattern was specifically created to address the complexity and non-scalablity of Threaded programming for IO bound code?
Rate this:
Please Sign up or sign in to vote.

Solution 2

// assuming you have created a CancellationToken so you can cancel the Telnet Monitors.

List<task> telnetTasks = new List<task>();

for (var i = 0; i < NumTelnetSessions; I++)
{
    var monitor = new TelnetMonitor(telnetAddresses[i]);
    telnetTasks.Add(monitor.Run(cancellationToken));
}

Task.WhenAll(telnetTasks.ToArray());

...
</task></task>

You can stop the monitors by cancelling the cancellationToken.

TelnetMonitor.Run is an async Task method that monitors the specified server in a loop awaiting async Send, Receive and Delay methods and checking for cancellation in the loop.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100