0

我试图通过以太网电缆与两台电脑通信。我已经进入设置并告诉它使用两个特定的 IP 地址。我已经关闭了两台电脑上的防火墙并设法从一台电脑ping到另一台电脑。当我尝试使用以下代码时,它不起作用。在指定的地址什么都听不到的东西。有任何想法吗?

//服务器

using System;
using System.ServiceModel;

namespace WCFServer
{
  [ServiceContract]
  public interface IStringReverser
  {
    [OperationContract]
    string ReverseString(string value);
  }

  public class StringReverser : IStringReverser
  {
    public string ReverseString(string value)
    {
      char[] retVal = value.ToCharArray();
      int idx = 0;
      for (int i = value.Length - 1; i >= 0; i--)
        retVal[idx++] = value[i];

      return new string(retVal);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      using (ServiceHost host = new ServiceHost(
        typeof(StringReverser),
        new Uri[]{
          new Uri("http://192.168.10.10")
        }))
      {

        host.AddServiceEndpoint(typeof(IStringReverser),
          new BasicHttpBinding(),
          "Reverse");

        host.Open();

        Console.WriteLine("Service is available. " +  
          "Press <ENTER> to exit.");
        Console.ReadLine();

        host.Close();
      }
    }
  }
}

//客户

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace WCFClient
{
  [ServiceContract]
  public interface IStringReverser
  {
    [OperationContract]
    string ReverseString(string value);
  }

  class Program
  {
    static void Main(string[] args)
    {
      ChannelFactory<IStringReverser> httpFactory =
         new ChannelFactory<IStringReverser>(
          new BasicHttpBinding(),
          new EndpointAddress(
            "http://192.168.10.9"));


      IStringReverser httpProxy =
        httpFactory.CreateChannel();

      while (true)
      {
        string str = Console.ReadLine();
        Console.WriteLine("http: " + 
          httpProxy.ReverseString(str));
      }
    }
  }
}
4

1 回答 1

2

您的服务正在侦听的地址是http://192.168.10.10/Reverse(您提供的 Uri 加上您提供的端点名称),您应该将您的客户端连接到此端点,而不是http://192.168.10.9.

于 2013-05-05T13:08:50.607 回答