C# 使用WCF http通过LAN进行通信

C# 使用WCF http通过LAN进行通信,c#,wcf,http,c#-4.0,distributed-computing,C#,Wcf,Http,C# 4.0,Distributed Computing,我正试图通过以太网电缆与两台电脑通信。我已经进入设置,并告诉它使用两个特定的ip地址。我关闭了两台电脑的防火墙,并成功地从一台电脑ping到另一台电脑。当我尝试使用下面的代码时,它不起作用。在指定的地址进行侦听时没有任何内容。有什么想法吗 //服务器 using System; using System.ServiceModel; namespace WCFServer { [ServiceContract] public interface IStringReverser {

我正试图通过以太网电缆与两台电脑通信。我已经进入设置,并告诉它使用两个特定的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();
      }
    }
  }
}
使用系统;
使用System.ServiceModel;
命名空间WCFServer
{
[服务合同]
公共接口IStringReverser
{
[经营合同]
字符串反向限制(字符串值);
}
公共类StringReverser:IStringReverser
{
公共字符串反向限制(字符串值)
{
char[]retVal=value.ToCharArray();
int-idx=0;
对于(int i=value.Length-1;i>=0;i--)
retVal[idx++]=值[i];
返回新字符串(retVal);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
使用(ServiceHost主机=新ServiceHost主机)(
类型(桁条反向器),
新Uri[]{
新Uri(“http://192.168.10.10")
}))
{
host.AddServiceEndpoint(typeof(IStringReverser),
新的BasicHttpBinding(),
“反向”);
host.Open();
Console.WriteLine(“服务可用。”+
“按退出”。);
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));
      }
    }
  }
}
使用系统;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
命名空间WCFClient
{
[服务合同]
公共接口IStringReverser
{
[经营合同]
字符串反向限制(字符串值);
}
班级计划
{
静态void Main(字符串[]参数)
{
ChannelFactory httpFactory=
新渠道工厂(
新的BasicHttpBinding(),
新端点地址(
"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

很抱歉,在这些事情上我有点生疏+我现在无法检查,因为我不在电脑上。因此,在客户端代码中,我需要指定另一台计算机的IP地址,即服务器。在服务器代码中,我需要输入与实际运行代码的计算机相同的IP地址?然后,我如何允许服务器首先调用客户机,或者客户机始终必须首先调用服务器?非常感谢你迄今为止的帮助。“Reverse”是您在服务器中为端点指定的名称:
host.AddServiceEndpoint(typeof(IStringReverser),new BasicHttpBinding(),“Reverse”)。请注意最后一个参数。这会附加到您的IP中,因此如果您多次调用AddServiceEndpoint,每个IP可以有多个端点。非常感谢您将其清除。有一件事我还是有点模糊。服务器只是在某个特定端口上侦听,直到客户端将信息发送到该特定端口。服务器如何知道将信息发送回哪个地址?服务器是否可以先调用客户端。我读了一些关于必须使用wsDualHttpBinding的内容。再次感谢您的时间。将响应发送回客户端是由主机自动处理的,您不需要跟踪您的请求来自何处。您不能先呼叫客户端。。。这将颠倒客户端和服务器的角色。如果你愿意,你可以有两台服务器。你有时间在聊天室回答几个问题吗?