Package java.net

Class MulticastSocket

java.lang.Object
java.net.DatagramSocket
java.net.MulticastSocket
All Implemented Interfaces:
Closeable, AutoCloseable

public class MulticastSocket
extends DatagramSocket
This class implements a multicast socket for sending and receiving IP multicast datagram packets.
See Also:
DatagramSocket
  • Constructor Details

    • MulticastSocket

      public MulticastSocket() throws IOException
      Constructs a multicast socket, bound to any available port on the local host.
      Throws:
      IOException - if an error occurs.
    • MulticastSocket

      public MulticastSocket​(int port) throws IOException
      Constructs a multicast socket, bound to the specified port on the local host.
      Throws:
      IOException - if an error occurs.
    • MulticastSocket

      public MulticastSocket​(SocketAddress localAddress) throws IOException
      Constructs a MulticastSocket bound to the address and port specified by localAddress, or an unbound MulticastSocket if localAddress == null.
      Throws:
      IllegalArgumentException - if localAddress is not supported (because it's not an InetSocketAddress, say).
      IOException - if an error occurs.
  • Method Details

    • getInterface

      public InetAddress getInterface() throws SocketException
      Returns an address of the outgoing network interface used by this socket. To avoid inherent unpredictability, new code should use getNetworkInterface() instead.
      Throws:
      SocketException - if an error occurs.
    • getNetworkInterface

      public NetworkInterface getNetworkInterface() throws SocketException
      Returns the outgoing network interface used by this socket.
      Throws:
      SocketException - if an error occurs.
    • getTimeToLive

      public int getTimeToLive() throws IOException
      Returns the time-to-live (TTL) for multicast packets sent on this socket.
      Throws:
      IOException - if an error occurs.
    • getTTL

      @Deprecated public byte getTTL() throws IOException
      Deprecated.
      Use getTimeToLive() instead.
      Returns the time-to-live (TTL) for multicast packets sent on this socket.
      Throws:
      IOException - if an error occurs.
    • joinGroup

      public void joinGroup​(InetAddress groupAddr) throws IOException
      Adds this socket to the specified multicast group. A socket must join a group before data may be received. A socket may be a member of multiple groups but may join any group only once.
      Parameters:
      groupAddr - the multicast group to be joined.
      Throws:
      IOException - if an error occurs.
    • joinGroup

      public void joinGroup​(SocketAddress groupAddress, NetworkInterface netInterface) throws IOException
      Adds this socket to the specified multicast group. A socket must join a group before data may be received. A socket may be a member of multiple groups but may join any group only once.
      Parameters:
      groupAddress - the multicast group to be joined.
      netInterface - the network interface on which the datagram packets will be received.
      Throws:
      IOException - if the specified address is not a multicast address.
      IllegalArgumentException - if no multicast group is specified.
    • leaveGroup

      public void leaveGroup​(InetAddress groupAddr) throws IOException
      Removes this socket from the specified multicast group.
      Parameters:
      groupAddr - the multicast group to be left.
      Throws:
      NullPointerException - if groupAddr is null.
      IOException - if the specified group address is not a multicast address.
    • leaveGroup

      public void leaveGroup​(SocketAddress groupAddress, NetworkInterface netInterface) throws IOException
      Removes this socket from the specified multicast group.
      Parameters:
      groupAddress - the multicast group to be left.
      netInterface - the network interface on which the addresses should be dropped.
      Throws:
      IOException - if the specified group address is not a multicast address.
      IllegalArgumentException - if groupAddress is null.
    • send

      @Deprecated public void send​(DatagramPacket packet, byte ttl) throws IOException
      Deprecated.
      Use setTimeToLive(int) instead.
      Sends the given packet on this socket, using the given ttl. This method is deprecated because it modifies the TTL socket option for this socket twice on each call.
      Throws:
      IOException - if an error occurs.
    • setInterface

      public void setInterface​(InetAddress address) throws SocketException
      Sets the outgoing network interface used by this socket. The interface used is the first interface found to have the given address. To avoid inherent unpredictability, new code should use getNetworkInterface() instead.
      Throws:
      SocketException - if an error occurs.
    • setNetworkInterface

      public void setNetworkInterface​(NetworkInterface networkInterface) throws SocketException
      Sets the outgoing network interface used by this socket to the given networkInterface.
      Overrides:
      setNetworkInterface in class DatagramSocket
      Throws:
      SocketException - if an error occurs.
    • setTimeToLive

      public void setTimeToLive​(int ttl) throws IOException
      Sets the time-to-live (TTL) for multicast packets sent on this socket. Valid TTL values are between 0 and 255 inclusive.
      Throws:
      IOException - if an error occurs.
    • setTTL

      @Deprecated public void setTTL​(byte ttl) throws IOException
      Deprecated.
      Use setTimeToLive(int) instead.
      Sets the time-to-live (TTL) for multicast packets sent on this socket. Valid TTL values are between 0 and 255 inclusive.
      Throws:
      IOException - if an error occurs.
    • getLoopbackMode

      public boolean getLoopbackMode() throws SocketException
      Returns true if multicast loopback is disabled. See SocketOptions.IP_MULTICAST_LOOP, and note that the sense of this is the opposite of the underlying Unix IP_MULTICAST_LOOP.
      Throws:
      SocketException - if an error occurs.
    • setLoopbackMode

      public void setLoopbackMode​(boolean disable) throws SocketException
      Disables multicast loopback if disable == true. See SocketOptions.IP_MULTICAST_LOOP, and note that the sense of this is the opposite of the underlying Unix IP_MULTICAST_LOOP.
      Throws:
      SocketException - if an error occurs.