2

我有这个工作,但我不明白如何预测连接何时断开并需要重新连接到服务器。

我想要做的是让客户端立即连接到 .net 远程服务器,如果它退出,有点像继续尝试连接并忽略它是否不需要。

帮助表示赞赏。

这是我的代码:

namespace TaskClient
{
    using System;
    using System.Reflection;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Threading;
    using TaskShared;

    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan, false);
            Connections remObject = (Connections)Activator.GetObject(typeof(Connections), "tcp://localhost:8085/TaskServer");

            if (remObject == null)
            {
                Console.WriteLine("Could not connect to TaskServer. (tcp://localhost:8085/TaskServer)");
            }
            else
            {
                Console.WriteLine("Connected to Task Server.");

                var rt = new TestTask() 
                {
                    ApplicationName = Assembly.GetExecutingAssembly().FullName,
                    ComputerName = Environment.MachineName,
                    VersionInfo = "1.0.0",
                    JobRunning = "None"
                };

                remObject.Add(rt);
            }


            while (true)
            {
                //TODO:
                //Check if connected.. how?
                //Re-create the connection... how?

                //doing this simple won't work:
                if (remObject == null)
                    remObject = (Connections)Activator.GetObject(typeof(Connections), "tcp://localhost:8085/TaskServer");



                remObject.Invalidate(remObject[rt]);

                Thread.Sleep(1000);
            }
        }
    }
}
4

0 回答 0