views:

99

answers:

2

Hi all -

I am writing an app to test RDP connectivity to a bank of VM Windows 2003 servers. The typical failure mode is for some code running on the server to get itself in a loop, preventing RDP connections -- and most any other type of connection, as well. When this happens, you can still ping the server and it seems to be fine, but you cannot do much of anything else.

I am using C#, .NET 3.5 and RDP version 6.1 -- I have tried launching the ActiveX RDP client (MSTSC..) but that requires human intervention and doesn't work very well. I found another post here that has a possible solution from Expert Sexchange, but that solution uses Java and the Net::Telnet library, which I do not have access to.

Any ideas from the .NET camp?

Thanks, Dave

A: 

Use a TCPClient to try and connect to the servers on 3389 (or whatever port you are configured for) if you get a connection, disconnect and report success, if the connection is refused report failure.

 class Program
    {
       static void Main(string[] args)
        {
            RDPAvailable("someserver", 3389);
        }
        public static bool RDPAvailable(string remoteHost, int port)
        {
            bool available=false;
            try
            {
                TcpClient client = new TcpClient(remoteHost, port);
                client.Close();
                available = true;
            }
            catch (Exception ex)
            {
                //do some logging or whatnot 
            }
            return available;
    }

EDIT: Some handy code

MsRdpClient51.Server = somServer
MsRdpClient51.UserName = somUserID
MsRdpClient51.Domain = someDomain
MsRdpClient51.AdvancedSettings6.ClearTextPassword = somePassword
MsRdpClient51.Connect
cmsjr
Do you happen to have a link to example code? (I don't want much, do I?)Dave
DaveN59
Never mind, I found it in VS help... Imagine that!
DaveN59
lol, Well here's a console example anyways ;)
cmsjr
Sorry, that didn't do it. I can connect with the TCPClient class just fine, thank you. Still can't connect to the failing servder with RDP. (I have one that is failed at the moment, so I'm trying anything I can while I have a good test case...)
DaveN59
Well, if the connection doesn't get refused, I'd suggest adding a reference to mstscax.dll, which will allow you to programatically create and connect rdp clients, which should give you more detailed exception info.
cmsjr
That was my first approach (see original post). There are several gotchas to doing it that way, not the least of which is the need for human interaction. It looks like I may have to re-examine that approach, though...
DaveN59
Sorry I missed that, but it can be done without human intervention, it's just poorly documented, see my edit for the code (adapted from vb) you would use to connect programatically sans human intervention.
cmsjr
Actually, I am very familiar with the MSTSCax code and how it works, not that you'd know that from this post. I intentionally stripped this post down to the minimum to keep it from becoming a novel. At this point, I am not going to spend any more time trying to solve the problem, I am going to write up a set of manual procedures for others to follow instead. I do appreciate your input, though, thanks for the help!
DaveN59
A: 

You can use the undocumented WinStationServerPing API to check connectivity to Terminal Server. Basically if this function succeeds it means that Terminal Server is available.

This is the (Delphi) signature of the function (exported from winsta.dll):

function WinStationServerPing(hServer: HANDLE): BOOLEAN; stdcall;
Remko
I don't really know if this will work or not, as I have changed jobs and don't have access to the original application. Still, it sounds like the best approach I've heard so far...
DaveN59