简体   繁体   中英

How to automatically discover Web Service by scanning LAN C#

I am consuming a web service (which was written in Java) in C#. The web service is available in LAN but at compile time I don't know the address of the server. I need to scan the network to find the IP address of the computer where the web service is hosted. 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?

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. Also you can use DiscoveryClient class (C#) to discovered services, it allow you define scopes and other specifications.

Is your C# application that consumes the data at a static location?

At build time, you could modify your Java application to reach out to the consuming service to provide an accurate location. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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