首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF点对点聊天

WCF点对点聊天
EN

Stack Overflow用户
提问于 2009-10-05 13:21:32
回答 1查看 2K关注 0票数 1

我为一个WCF P2P聊天程序写了一些代码。

代码语言:javascript
复制
<services>
  <service name="PeerChat.Form1">
    <host>
      <baseAddresses>
        <add baseAddress="net.p2p://PeerChat/" />
      </baseAddresses>
    </host>
    <endpoint name="PeerChatEndPoint" address="" binding="netPeerTcpBinding" bindingConfiguration="BindingUnsecure"
       contract="PeerChat.IChatService" />
  </service>
</services>
<bindings>
  <netPeerTcpBinding>
    <binding name="BindingUnsecure">
      <resolver mode="Pnrp" />
      <security mode="None" />
    </binding>
  </netPeerTcpBinding>
</bindings>
<client>
  <endpoint
      name="PeerChatClientEndPoint"
      address="net.p2p://PeerChat/"
      binding="netPeerTcpBinding"
      bindingConfiguration="BindingUnsecure"
      contract="PeerChat.IChatService"
  />
</client>

然后我托管服务,如下所示:

代码语言:javascript
复制
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public partial class Form1 : Form, IChatService
{

    IChatService channel;
    ServiceHost host = null;
    ChannelFactory<IChatService> channelFactory = null;

    private void StartService()
    {
        //Instantiate new ServiceHost
        host = new ServiceHost(this);
        //Open ServiceHost
        host.Open();
        //Create a ChannelFactory and load the configuration setting
        channelFactory = new ChannelFactory<IChatService>("PeerChatClientEndPoint");
        channel = channelFactory.CreateChannel();
        //Lets others know that someone new has joined
        channel.SendMessage("Hello."+ Environment.NewLine);

        foreach (var cloud in Cloud.GetAvailableClouds())
        {
            textBox2.Text += cloud.Name + Environment.NewLine;
        }
    }
    private void StopService()
    {
        if (host != null)
        {
            channel.SendMessage("Bye." + Environment.NewLine);
            if (host.State != CommunicationState.Closed)
            {
                channelFactory.Close();
                host.Close();
            }
        }
    }

问题是我可以向程序的同一实例发送消息,但不能向另一个实例发送消息。Ie实例只接收自己的消息,不接收来自其他实例的消息。不确定是否是正确配置PNRP的问题?我在Windows7上测试过。

EN

回答 1

Stack Overflow用户

发布于 2009-10-05 13:31:02

您不会碰巧有两个程序实例监听相同的端点,对吗?我不确定,但我怀疑可能发生的情况是,您的客户端应用程序首先在端点上注册自己,然后在第二个端点获得消息之前拦截到达该端点的所有消息。我建议尝试将第二个实例配置为在具有不同Uri的端点上启动。假设一个连接到net.p2p://PeerChatA/,另一个连接到net.p2p://PeerChatB/。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1520038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档