Java networking Code - conversion to C#
-
Hi All, I've beenconverting some java classes to C# and am getting buffeted by headwinds with the network code conversion. Here is a block of code that has me at a loss for now:
try { Enumeration; interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { Enumeration addresses = interfaces.nextElement().getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (!addr.isLoopbackAddress()) { address = addr.getAddress(); } } } }
I've interpreted the statement:
Enumeration<NetworkInterface>; interfaces = NetworkInterface.getNetworkInterfaces();
as:
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
...which return a list of network interfaces on the host. However, the statement:
Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
...is giving me more trouble. I know the type that I want to create (IPHostEntry - I believe that's correct) but I can see no way to identify the IP Addresses associated with a given network interface (when the host is multi-homed). The C# classes that I have seen all seem to generically assume that the host has 1 interface which may have more than 1 IP Address. So my questions is, in a nutshell, if a host has multiple network interfaces how does one identify the ip addresses associated with each interface? All suggestions warmly welcomed...
Regards, Dave
-
Hi All, I've beenconverting some java classes to C# and am getting buffeted by headwinds with the network code conversion. Here is a block of code that has me at a loss for now:
try { Enumeration; interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { Enumeration addresses = interfaces.nextElement().getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (!addr.isLoopbackAddress()) { address = addr.getAddress(); } } } }
I've interpreted the statement:
Enumeration<NetworkInterface>; interfaces = NetworkInterface.getNetworkInterfaces();
as:
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
...which return a list of network interfaces on the host. However, the statement:
Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
...is giving me more trouble. I know the type that I want to create (IPHostEntry - I believe that's correct) but I can see no way to identify the IP Addresses associated with a given network interface (when the host is multi-homed). The C# classes that I have seen all seem to generically assume that the host has 1 interface which may have more than 1 IP Address. So my questions is, in a nutshell, if a host has multiple network interfaces how does one identify the ip addresses associated with each interface? All suggestions warmly welcomed...
Regards, Dave
DwR wrote:
So my questions is, in a nutshell, if a host has multiple network interfaces how does one identify the ip addresses associated with each interface?
Well your question does not seem to reflect the block of Java you posted but the NetworkInterface [^]and IPInterfaceProperties [^] examples in their documentation seem to answer your question.
led mike
-
Hi All, I've beenconverting some java classes to C# and am getting buffeted by headwinds with the network code conversion. Here is a block of code that has me at a loss for now:
try { Enumeration; interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { Enumeration addresses = interfaces.nextElement().getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (!addr.isLoopbackAddress()) { address = addr.getAddress(); } } } }
I've interpreted the statement:
Enumeration<NetworkInterface>; interfaces = NetworkInterface.getNetworkInterfaces();
as:
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
...which return a list of network interfaces on the host. However, the statement:
Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
...is giving me more trouble. I know the type that I want to create (IPHostEntry - I believe that's correct) but I can see no way to identify the IP Addresses associated with a given network interface (when the host is multi-homed). The C# classes that I have seen all seem to generically assume that the host has 1 interface which may have more than 1 IP Address. So my questions is, in a nutshell, if a host has multiple network interfaces how does one identify the ip addresses associated with each interface? All suggestions warmly welcomed...
Regards, Dave
Look at the NetworkInterface.GetIPProperties().UnicastAddresses[^] property.
-
DwR wrote:
So my questions is, in a nutshell, if a host has multiple network interfaces how does one identify the ip addresses associated with each interface?
Well your question does not seem to reflect the block of Java you posted but the NetworkInterface [^]and IPInterfaceProperties [^] examples in their documentation seem to answer your question.
led mike
-
As I'm not a java developer what is your interpretation of what that block of code is doing? That's one the problems I have. I'm trying to convert the code without being able to interactively debug it to see what it does.
Regards, Dave
DwR wrote:
what is your interpretation of what that block of code is doing?
It is looping through all of the network adapters and all of the addresses in each adapter and filtering out the loopback address/adapter. Finally the last ( well each one in turn without any exiting of the loops therefore the last one) adapter.address in the enumerations that is NOT the loopback adapter is assigned to the variable
address
, which is not declared in the code you posted so I have no idea what it is.address = addr.getAddress();
led mike