Task: on the computer when launching a remote application, check the availability of the remote application server and if the verification is successful, launch the application. The easiest way to send an echo request to a remote server. But this method is not the best since the server or service can hang or even an echo request can be denied in the firewall. Putting your application to open a socket on a terminal server is not an option.

Tell me how you can check the availability of the server on the port (in this case 3389)

    2 answers 2

    In any case, you can try to connect to the server, the error code will contain the reason why you could not connect (for example, if the remote host does not respond to the request (the host is not available or the address is incorrectly specified) or the remote machine rejected the connection request. T According to the context of the error, it is technically possible to guess the reason why the machine refused to connect, you can even have a separate asynchronous socket for this, which will constantly poll the remote system and adjust the logic of the main server. I once solved a similar problem by stating states, in case the remote machine rejected the connection request, I just try to reconnect after a certain time interval and write to the log. If the number of erroneous connections was greater than n, performed reconnection to backup addresses. Actually the whole system was asynchronous, so it was easy to resolve errors.

    • How can this be implemented on c #? - polsok
    • one
      Well, look, for example, you can create an object of class tcpclient, and make an asynchronous connection to the server (in this case, you will not block the main flow of the program). If the connection was successful, you set the state to connected, and work with the system, while monitoring the results of the send, recv operations. If they ended with an error that says that the remote host is not responding, change the state to disconnected, while the program tries to reconnect, etc. - QuaternioNoir

    actually here is the working code

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { // Создаем новый экземпляр TcpClient TcpClient newClient = new TcpClient(); // Устанавливаем соединение с IPEndPoint IPAddress ipAddr = IPAddress.Parse("10.50.0.9"); IPEndPoint endPoint = new IPEndPoint(ipAddr, 3389); // Соединяемся с хостом newClient.Connect(endPoint); } catch (SocketException ex) { Console.WriteLine("Exception: " + ex.ToString()); ; } Console.ReadKey(); } } } 
    • one
      In general, yes, but you will still need to add asynchronous connection and handlers for send, recv, directly read better async tcpclient example. - QuaternioNoir