我正在尝试让两个应用程序使用 HTTP/WCF 通过本地网络进行通信。 Master 发出 Web 请求并查找每个都运行 Web 服务的 Slave 应用程序。从站配置为响应 localhost:\\[MACHINENAME]:8000
当我在与主服务器相同的计算机上运行从服务器时它工作,但当我在同一网络上的另一台计算机上运行它时它不工作。我通过 cmd 提示符 Ping [MachineName] 确认计算机在同一网络上。向同一网络上的另一台计算机发送请求需要什么?
从机设置webservice:
public void Run()
{
Config config = Config.validateAndCreate();
string machineName = System.Environment.MachineName;
string baseAddress = "http://" + machineName + ":" + config.port;
Service.setConfig(config);
if (new Service().UpdateScripts().status != ExecStatus.OK)
{
throw new Exception("Failed to update scripts");
}
using (WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress)))
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
host.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageEnabled = false;
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
host.Open();
log.Info("Service is running at: " + baseAddress);
log.Info("Press the 'q' key to quit...");
while (Console.ReadKey(true).Key != ConsoleKey.Q) { }
host.Close();
}
}
最佳答案
最有可能阻止它的是防火墙。如果您没有添加第 3 方防火墙,则 Windows 防火墙(默认情况下处于启用状态)将阻止它。
您需要向防火墙添加异常(exception),以允许该端口上的流量路由到您的应用程序。
单击“开始”并键入“高级安全 Windows 防火墙”。
单击“入站规则”-> 新建规则,然后根据需要添加程序规则或端口规则。
关于c# - 在同一网络上通信的计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306329/