我在学习Remoting 的时,我想把一个程序做成即是服务器又是客户端,
那么就去注册两个TCP ,一个用了监听客户端,一个用于和另一个服务器端进行通信
程序大致如下:
 TcpChannel tcpService = new TcpChannel(8089);
            ChannelServices.RegisterChannel(tcpService );
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(showMessage.showmsg), "tcpChart", WellKnownObjectMode.Singleton);
            /*******************/
            TcpChannel  tcpClient = new TcpChannel();
            ChannelServices.RegisterChannel(tcpClient );            showMessage.showmsg msg = (showMessage.showmsg ) Activator.GetObject(typeof(showMessage.showmsg),"tcp://localhost:8088/tcpChart");
            while(true)
            {
                string msgstr  =  Console.ReadLine();
                msg.show(msgstr);
            }
可是当再去注册第二个TCP时出错: TCP通道已注册。 
请高手帮我解释一下,是程序问题呢? 还是其它的原因? 为什么不能注册两个TCP通道

解决方案 »

  1.   

    没用过Remoting,不过我估计可能是端口的问题,一个端口只能绑定一次,你可以换其他端口再尝试去注册看看。
      

  2.   


    我知道呀,我没有注册多个端口哟!? 不知道为什么?另外:我加上其它的端口也没有用?
    刚试过上面的程序其实可以不用再去注册通道了! 就那一个通道就可以实现监听和发送的功能! 
    如下:
    server1:
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels.Tcp;
    //using System.Runtime.Remoting.Channels.Http;
    using System.Runtime.Remoting.Channels;namespace remoting_p2p
    {
        class Program
        {
            static void Main(string[] args)
            {
                string msg=string.Empty;
                TcpChannel tcp = new TcpChannel(8099);
                ChannelServices.RegisterChannel(tcp);
                /***************server***********************/
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "chart", WellKnownObjectMode.Singleton);            /***************Client***********************/
                deal.deal show = (deal.deal)Activator.GetObject(typeof(deal.deal), "tcp://localhost:8088/Chart");
                while(true )
                {
                    msg =Console.ReadLine();
                    show.show(msg);
                }
            }
        }
    }
    server2:
    static void Main(string[] args)
            {
                string msg = string.Empty;
                TcpChannel tcp = new TcpChannel(8088);
                ChannelServices.RegisterChannel(tcp);
                /***************server***********************/
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "chart", WellKnownObjectMode.Singleton);
                deal.deal show = (deal.deal)Activator.GetObject(typeof(deal.deal), "tcp://localhost:8099/Chart");
                while (true)
                {
                    msg = Console.ReadLine();
                    show.show(msg);
                }
            }
    class:
    namespace deal
    {
        public class deal:System.MarshalByRefObject 
        {
            public void show(string _message)
            {
                Console.WriteLine("Server:" +_message);
            }
        }}
      

  3.   

    另外还可以注册多个服务:
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "Visitor", WellKnownObjectMode.Singleton);
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "Admin", WellKnownObjectMode.Singleton);
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(deal.deal), "Console", WellKnownObjectMode.Singleton);
      

  4.   

    几个比较好的Remoting 学习站点:
    http://blog.163.com/sdaizb/blog/static/161127382007111942352617/
    http://blog.csdn.net/21aspnet/category/285347.aspx