Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Java networking Code - conversion to C#

Java networking Code - conversion to C#

Scheduled Pinned Locked Moved C#
csharpjavasysadminquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DwR
    wrote on last edited by
    #1

    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

    L J 2 Replies Last reply
    0
    • D DwR

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • D DwR

        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

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        Look at the NetworkInterface.GetIPProperties().UnicastAddresses[^] property.

        1 Reply Last reply
        0
        • L led mike

          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

          D Offline
          D Offline
          DwR
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • D DwR

            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

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups