使用WCF http在局域网上进行通信

本文关键字:通信 局域网 WCF http 使用 | 更新日期: 2023-09-27 18:12:06

我试图通过以太网电缆与两台pc进行通信。我已经进入设置并告诉它使用两个特定的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));
      }
    }
  }
}

使用WCF http在局域网上进行通信

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