Class UdpConnection


  • public abstract class UdpConnection
    extends Object
    A connection to a udp port
    Version:
    1.3
    Author:
    man
    • Constructor Detail

      • UdpConnection

        public UdpConnection()
    • Method Detail

      • getAllInterfaceInetAddresses

        public static List<InetAddress> getAllInterfaceInetAddresses()
                                                              throws SocketException
        Get the InetAddresses of all local network interfaces. Includes only IPv4 addresses and only real interface addresses (no local hosts etc.). It is important to keep this list as small as possible since it is also the list we send broadcasts on.
        Returns:
        List of addresses
        Throws:
        SocketException - if an I/O error occurs.
      • sendBroadcastRecvOnUnicast

        public static void sendBroadcastRecvOnUnicast​(DatagramPacket sendPacket,
                                                      int srcPort,
                                                      int recvLength,
                                                      int timeout,
                                                      List<InetAddress> addresses,
                                                      UdpBroadcastHandlerInterface handler)
                                               throws IOException
        Send broadcast packets from a list of source addresses and receive answers on the corresponding unicast addresses.

        This can be used whenever a device responds to a broadcast packet with an unicast answer.

        It can and must also be used on Windows to receive broadcast answers. However you should not use this method in portable code that needs to receive broadcast answers since different operating systems need different tricks to receive broadcast responses and associate them with specific local interfaces. Always use sendRecvBroadcast(DatagramPacket, int, int, int, List, UdpBroadcastHandlerInterface) for that purpose.

        Parameters:
        sendPacket - Packet to broadcast. The destination address is set to 255.255.255.255 automatically. If this is null, no packet is sent.
        srcPort - Source port of outbound broadcasts and used to listen for responses.
        recvLength - Length of response packets. This will be the length of returned packets' data buffers. If less than or equal 0, do not try to receive any packets.
        timeout - Time to wait for responses in milliseconds.
        addresses - Addresses to bind to when broadcasting. This effectively determines the interfaces that sendPacket will be broadcast on.
        handler - A functor object for handling received responses/packets.
        Throws:
        IOException - if an unexpected I/O error occured.
      • sendRecvBroadcast

        public static void sendRecvBroadcast​(DatagramPacket sendPacket,
                                             int srcPort,
                                             int recvLength,
                                             int timeout,
                                             UdpBroadcastHandlerInterface handler)
                                      throws IOException
        Send broadcast packets from all local interface addresses and receive broadcast answers in a platform-independant manner.
        Parameters:
        sendPacket - Packet to broadcast. The destination address is set to 255.255.255.255 automatically. If this is null, no packet is sent.
        srcPort - Source port of outbound broadcasts and used for listening for broadcasts.
        recvLength - Length of broadcast responses. This will be the length of returned packets' data buffers. If less than or equal 0, do not try to receive any packets.
        timeout - Time to wait for responses in milliseconds.
        handler - A functor object for handling received broadcast responses/packets.
        Throws:
        IOException - if an unexpected I/O error occured.
      • sendRecvBroadcast

        public static void sendRecvBroadcast​(DatagramPacket sendPacket,
                                             int srcPort,
                                             int recvLength,
                                             int timeout,
                                             InetAddress addr,
                                             UdpBroadcastHandlerInterface handler)
                                      throws IOException
        Send broadcast packets from a single source source address and receive broadcast answers in a platform-independant manner.
        Parameters:
        sendPacket - Packet to broadcast. The destination address is set to 255.255.255.255 automatically. If this is null, no packet is sent.
        srcPort - Source port of outbound broadcasts and used for listening for broadcasts.
        recvLength - Length of broadcast responses. This will be the length of returned packets' data buffers. If less than or equal 0, do not try to receive any packets.
        timeout - Time to wait for responses in milliseconds.
        addr - Address to bind to when broadcasting. This effectively determines the interface that sendPacket will be broadcast on.
        handler - A functor object for handling received broadcast responses/packets.
        Throws:
        IOException - if an unexpected I/O error occured.
      • sendRecvBroadcast

        public static void sendRecvBroadcast​(DatagramPacket sendPacket,
                                             int srcPort,
                                             int recvLength,
                                             int timeout,
                                             List<InetAddress> addresses,
                                             UdpBroadcastHandlerInterface handler)
                                      throws IOException
        Send broadcast packets from a list of source addresses and receive broadcast answers in a platform-independant manner.
        Parameters:
        sendPacket - Packet to broadcast. The destination address is set to 255.255.255.255 automatically. If this is null, no packet is sent.
        srcPort - Source port of outbound broadcasts and used for listening for broadcasts.
        recvLength - Length of broadcast responses. This will be the length of returned packets' data buffers. If less than or equal 0, do not try to receive any packets.
        timeout - Time to wait for responses in milliseconds.
        addresses - Addresses to bind to when broadcasting. This effectively determines the interfaces that sendPacket will be broadcast on.
        handler - A functor object for handling received broadcast responses/packets.
        Throws:
        IOException - if an unexpected I/O error occured.
      • getLantronixEthernetDevices

        public static List<EthernetDevice> getLantronixEthernetDevices​(int timeout)
                                                                throws IOException
        Send UDP broadcasts on all local network interfaces and return all responding Lantronix devices.
        Parameters:
        timeout - Time to wait for responses in milliseconds.
        Returns:
        List of Lantronix devices. It is already sorted.
        Throws:
        IOException - if an unexpected I/O error occurs.
      • getLantronixEthernetDevices

        public static List<EthernetDevice> getLantronixEthernetDevices​(int timeout,
                                                                       InetAddress addr)
                                                                throws IOException
        Send UDP broadcast from a specific IP address and return all responding Lantronix devices.
        Parameters:
        timeout - Time to wait for responses in milliseconds.
        addr - Address to bind to, i.e. address of the interface to send broadcast on.
        Returns:
        List of Lantronix devices. It is already sorted.
        Throws:
        IOException - if an unexpected I/O error occurs.
      • getLantronixEthernetDevices

        public static List<EthernetDevice> getLantronixEthernetDevices​(int timeout,
                                                                       List<InetAddress> addresses)
                                                                throws IOException
        Send UDP broadcasts from a specified list of IP addresses and return all responding Lantronix devices.
        Parameters:
        timeout - Time to wait for responses in milliseconds.
        addresses - Addresses to bind to when broadcasting. This effectively determines the interfaces that sendPacket will be broadcast on.
        Returns:
        List of Lantronix devices. It is already sorted.
        Throws:
        IOException - if an unexpected I/O error occurs.
      • getMetratecEthernetDevices

        public static List<EthernetDevice> getMetratecEthernetDevices​(int timeout)
                                                               throws IOException
        Send UDP broadcasts on all local network interfaces and return all responding metraTec TUC devices.
        Parameters:
        timeout - Time to wait for responses in milliseconds.
        Returns:
        List of TUC devices. It is sorted already.
        Throws:
        IOException - if an unexpected I/O error occurs.
      • getMetratecEthernetDevices

        public static List<EthernetDevice> getMetratecEthernetDevices​(int timeout,
                                                                      InetAddress addr)
                                                               throws IOException
        Send UDP broadcast from a specific IP address and return all responding metraTec TUC devices.
        Parameters:
        timeout - Time to wait for responses in milliseconds.
        addr - Address to bind to, i.e. address of the interface to send broadcast on.
        Returns:
        List of TUC devices. It is sorted already.
        Throws:
        IOException - if an unexpected I/O error occurs.
      • getMetratecEthernetDevices

        public static List<EthernetDevice> getMetratecEthernetDevices​(int timeout,
                                                                      List<InetAddress> addresses)
                                                               throws IOException
        Send UDP broadcasts from a specified list of IP addresses and return all responding metraTec TUC devices.
        Parameters:
        timeout - Time to wait for responses in milliseconds.
        addresses - Addresses to bind to when broadcasting. This effectively determines the interfaces that sendPacket will be broadcast on.
        Returns:
        List of TUC devices. It is sorted already.
        Throws:
        IOException - if an unexpected I/O error occurs.