小弟想用remoting 方法编写一个程序。可是每次在server 运行完后都要给server 重新指定端口
不然的话启动程序就会出现socket 异常。如果重新分配一个端口就好了
我仔细检查了服务器的程序,channel 确实已经释放过了,可是就是不行。请大侠们指教,小弟在此写过。Hello:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Test
{
    public class Hello:System.MarshalByRefObject
    {        private TcpServerChannel _channel;
        public Hello()
        {
           
            Console.WriteLine("construct called");
        }        ~Hello()
        {
            Console.WriteLine("Destructor called");
        }        public string Greeting(string name)
        {
            Console.WriteLine("Greeting called");
            return "hello" + name;
        }        public void start()
        {
             
            this._channel = new TcpServerChannel(20006);
            ChannelServices.RegisterChannel(this._channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
         
            
        }        public void stop()
        {
            try
            {                            this._channel.StopListening(null);
                ChannelServices.UnregisterChannel(this._channel);                          }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Can't Unregister channel! " + ex.ToString());
            }
        }
    }
}Server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            
          
            Hello m_hello = new Hello();            m_hello.start();            System.Console.WriteLine("press return to exit");
            System.Console.ReadLine();            m_hello.stop();
            Console.WriteLine("server stop");           
        }
    }
}Client 
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Test;namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel());            Hello obj = (Hello)Activator.GetObject(typeofHello), "tcp://localhost:20006/Hi");            for (int i = 0; i < 10; i++)
                Console.WriteLine(obj.Greeting("howard"));            Console.ReadLine();        }
    }
}