/**
   * Joins a multicast group. Its behavior may be affected by {@code setInterface} or {@code
   * setNetworkInterface}.
   *
   * <p>If there is a security manager, this method first calls its {@code checkMulticast} method
   * with the {@code mcastaddr} argument as its argument.
   *
   * @param mcastaddr is the multicast address to join
   * @exception IOException if there is an error joining or when the address is not a multicast
   *     address.
   * @exception SecurityException if a security manager exists and its {@code checkMulticast} method
   *     doesn't allow the join.
   * @see SecurityManager#checkMulticast(InetAddress)
   */
  public void joinGroup(InetAddress mcastaddr) throws IOException {
    if (isClosed()) {
      throw new SocketException("Socket is closed");
    }

    checkAddress(mcastaddr, "joinGroup");
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
      security.checkMulticast(mcastaddr);
    }

    if (!mcastaddr.isMulticastAddress()) {
      throw new SocketException("Not a multicast address");
    }

    /**
     * required for some platforms where it's not possible to join a group without setting the
     * interface first.
     */
    NetworkInterface defaultInterface = NetworkInterface.getDefault();

    if (!interfaceSet && defaultInterface != null) {
      setNetworkInterface(defaultInterface);
    }

    getImpl().join(mcastaddr);
  }
Beispiel #2
0
  /** Publish a request message to the specified multicast group. */
  protected void publishRequest(String message) throws IOException, SocketException {

    groupAddr = InetAddress.getByName(request_group);
    interfaceAddr = InetAddress.getByName(request_interface_address);

    /* is it a multicast address? */
    if (groupAddr.isMulticastAddress()) {

      /* open the socket and join the multicast group */
      udpSocket = new MulticastSocket();
      udpSocket.setNetworkInterface(NetworkInterface.getByInetAddress(interfaceAddr));
      udpSocket.setInterface(interfaceAddr);
      udpSocket.joinGroup(groupAddr);

      /* Send request packet */
      DatagramPacket p =
          new DatagramPacket(message.getBytes(), message.getBytes().length, groupAddr, 7777);

      System.out.println("Sending request: " + new String(p.getData(), 0, p.getLength()));
      udpSocket.send(p);

    } else {
      System.err.println("Invalid multicast address: " + groupAddr.toString());
    }
  }
 private void createSocket() throws IOException {
   if (this.getTheSocket() == null) {
     MulticastSocket socket;
     if (this.isAcknowledge()) {
       if (logger.isDebugEnabled()) {
         logger.debug("Listening for acks on port: " + this.getAckPort());
       }
       if (localAddress == null) {
         socket = new MulticastSocket(this.getAckPort());
       } else {
         InetAddress whichNic = InetAddress.getByName(this.localAddress);
         socket = new MulticastSocket(new InetSocketAddress(whichNic, this.getAckPort()));
       }
       if (this.getSoReceiveBufferSize() > 0) {
         socket.setReceiveBufferSize(this.getSoReceiveBufferSize());
       }
     } else {
       socket = new MulticastSocket();
     }
     if (this.timeToLive >= 0) {
       socket.setTimeToLive(this.timeToLive);
     }
     setSocketAttributes(socket);
     if (localAddress != null) {
       InetAddress whichNic = InetAddress.getByName(this.localAddress);
       NetworkInterface intfce = NetworkInterface.getByInetAddress(whichNic);
       socket.setNetworkInterface(intfce);
     }
     this.setSocket(socket);
   }
 }
 @Override
 public void setNetworkInterface(NetworkInterface networkInterface) {
   if (socket instanceof MulticastSocket) {
     try {
       ((MulticastSocket) socket).setNetworkInterface(networkInterface);
     } catch (SocketException e) {
       throw new ChannelException(e);
     }
   } else {
     throw new UnsupportedOperationException();
   }
 }
  /**
   * Creates an array of sockets with TTL and network interface set.
   *
   * @param mcastTTL multicast TTL.
   * @return an array of multicast sockets to broadcast on.
   * @throws IOException if I/O error occurred while creating a multicast socket.
   * @noinspection SocketOpenedButNotSafelyClosed, ConstantConditions
   */
  private static MulticastSocket[] createSockets(final int mcastTTL) throws IOException {

    Exception lastException = null; // Records last error in case we could not create any sockets
    final List<MulticastSocket> socketList = new ArrayList<MulticastSocket>(11);
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    while (enumeration.hasMoreElements()) {
      try {
        final NetworkInterface netIf = enumeration.nextElement();
        final MulticastSocket socket = new MulticastSocket(); // NOPMD
        socket.setTimeToLive(mcastTTL);
        socket.setNetworkInterface(netIf);
        socket.setSendBufferSize(SEND_BUFFER_SIZE);
        socketList.add(socket);
      } catch (final Exception e) {
        lastException = e;
        ExceptionUtils.ignoreException(e, "continue to connect to those we can");
      }
    }
    if (socketList.isEmpty()) {
      throw new IOException(
          "Could not create at least one multicast socket. Last error: " + lastException);
    }
    return socketList.toArray(new MulticastSocket[socketList.size()]);
  }