none
请教:WCF + P2P 的问题 RRS feed

  • 问题

  • public class Peer
        {
            public event Action<string, string> MessageReceived;

            private Service service;
            private IService channel;
            private DuplexChannelFactory<IService> factory;

           
            public Peer()
            {
                var binding = new NetPeerTcpBinding();
                binding.Security.Mode = SecurityMode.None;
                binding.Port = 0;
                binding.Resolver.Mode = System.ServiceModel.PeerResolvers.PeerResolverMode.Pnrp;
                var endpoint = new ServiceEndpoint(
                    ContractDescription.GetContract(typeof(IService)),
                    binding,
                    new EndpointAddress("net.p2p://denver.wcf.test"));
                this.service = new Service();
                this.service.MessageReceived += new Action<string, string>(service_MessageReceived);
                this.factory = new DuplexChannelFactory<IService>(new InstanceContext(this.service), endpoint);
                this.channel = this.factory.CreateChannel();
            }

            public void Start()
            {
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.ThreadStart));
            }

            private void ThreadStart(object obj)
            {
                (this.channel as ICommunicationObject).Open();
            }

            public void Stop()
            {
                (this.channel as ICommunicationObject).Close();
            }

            public void SendMessage(string msg)
            {
                this.channel.SendMessage(System.Net.Dns.GetHostName(), msg);
            }

            void service_MessageReceived(string arg1, string arg2)
            {
                if (this.MessageReceived != null)
                {
                    this.MessageReceived(arg1, arg2);
                }
            }
        }

    写了个WCF的测试程序,使用P2P绑定,想实现类似无服务器的聊天功能,

    但是最后发现在同一台计算上运行多个实例,功能没有问题,但是,同一个局域网中的另外一台计算机,

    互相都收不到消息,比如 HOST A 192.168.1.2, HOST B 192.168.1.4,每个主机上运行两个实例,

    HOST A或者HOST B 都只能收到自己主机上的消息...

    找了很久也找不到原因,防火墙关闭也不行,高手指点一下吧?

    操作系统均为 Win7 旗舰版

     

    2010年9月9日 3:16

全部回复

  • 换一个 NetPeerTcpBinding.Port 看看,然后把防火墙对此 Port 打开 Exception。
    Mark Zhou
    2010年9月9日 7:25
  • 换一个 NetPeerTcpBinding.Port 看看,然后把防火墙对此 Port 打开 Exception。
    Mark Zhou


    感谢回复...

    我试了一下,端口换成4000,防火墙开放TCP和UDP 4000端口,还是不行...

    其中一台机器我还关闭了防火墙,另外一台建了3个规则,TCP,UDP,应用程序.

    问题依旧...我就纳闷了,怎么就是不行呢?

    2010年9月9日 12:47