简体   繁体   English

使用WCF http通过LAN进行通信

[英]Using WCF http to communicate over a LAN

Im trying to communicate with two pc's via an ethernet cable. 我试图通过以太网电缆与两台电脑进行通信。 I've gone into the settings and told it to use two specific ip addresses. 我已经进入设置并告诉它使用两个特定的IP地址。 Ive turned the firewalls off on both pc's and managed to ping from one pc to the other. 我已经在两台电脑上关闭防火墙,并设法从一台电脑ping到另一台电脑。 When i try and use the following code, its not working though. 当我尝试使用以下代码时,它不工作。 Something about nothing listening at the specified address. 什么都没有在指定的地址听。 Any ideas? 有任何想法吗?

//SERVER //服务器

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();
      }
    }
  }
}

//CLIENT //客户

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));
      }
    }
  }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM