简体   繁体   English

如何通过扫描LAN C#自动发现Web服务

[英]How to automatically discover Web Service by scanning LAN C#

I am consuming a web service (which was written in Java) in C#. 我正在用C#消费Web服务(用Java编写)。 The web service is available in LAN but at compile time I don't know the address of the server. Web服务可在LAN中使用,但在编译时我不知道服务器的地址。 I need to scan the network to find the IP address of the computer where the web service is hosted. 我需要扫描网络以找到托管Web服务的计算机的IP地址。 I do know the port though. 我确实知道港口。

At the moment, I have the following code, which is supposed to give me a list of all active IP address and then I am planning trying to find out if the write port is open, and as soon as I find a computer where the right port is open, I will know the IP address of the server, am I thinking along the correct lines? 目前,我有以下代码,应该为我提供所有活动IP地址的列表,然后我计划尝试查找写入端口是否打开,并且一旦找到一台正确的计算机,端口是开放的,我会知道服务器的IP地址,我在想正确的方法吗?

List<IPAddress> ipList = new List<IPAddress>();
IPGlobalProperties network = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = network.GetActiveTcpConnections();

foreach (TcpConnectionInformation ipEnds in connections)
{
    ipList.Add(ipEnds.LocalEndPoint.Address);
    Console.WriteLine(ipEnds.LocalEndPoint.Address.ToString());
}

The web-services must have define the way you can discovered, for example you throw a broadcast with a specific soap message, in that way al services that matched with that soap message must response to you. Web服务必须定义您发现的方式,例如,您使用特定的肥皂消息引发广播,以这种方式,与该肥皂消息匹配的所有服务都必须响应您。 Also you can use DiscoveryClient class (C#) to discovered services, it allow you define scopes and other specifications. 您还可以使用DiscoveryClient类(C#)来发现服务,它允许您定义范围和其他规范。

Is your C# application that consumes the data at a static location? 您的C#应用​​程序是否在静态位置使用数据?

At build time, you could modify your Java application to reach out to the consuming service to provide an accurate location. 在构建时,您可以修改Java应用程序以访问使用服务以提供准确的位置。 This should reduce extra noise on your network. 这样可以减少网络上的额外噪音。 This should also reduce time that your application will hang attempting to connect to each IP address on the specified port. 这还应减少您的应用程序尝试连接到指定端口上的每个IP地址时挂起的时间。

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

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