List of usage examples for java.net MulticastSocket getNetworkInterface
public NetworkInterface getNetworkInterface() throws SocketException
From source file:Main.java
public static void main(String[] args) throws Exception { int port = 0; byte ttl = (byte) 1; InetAddress ia = InetAddress.getByName("127.0.0.1"); byte[] data = "Here's some multicast data\r\n".getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, ia, port); MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080)); ms.joinGroup(ia);// www. ja v a2s .com for (int i = 1; i < 10; i++) { ms.send(dp, ttl); } ms.leaveGroup(ia); System.out.println(ms.getNetworkInterface().getDisplayName()); ms.close(); }
From source file:net.pms.network.UPNPHelper.java
/** * Gets the new multicast socket.//from w w w . j ava2 s . co m * * @return the new multicast socket * @throws IOException Signals that an I/O exception has occurred. */ private static MulticastSocket getNewMulticastSocket() throws IOException { NetworkInterface networkInterface = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName(); if (networkInterface == null) { networkInterface = PMS.get().getServer().getNetworkInterface(); } if (networkInterface == null) { throw new IOException("No usable network interface found for UPnP multicast"); } List<InetAddress> usableAddresses = new ArrayList<InetAddress>(); List<InetAddress> networkInterfaceAddresses = Collections.list(networkInterface.getInetAddresses()); for (InetAddress inetAddress : networkInterfaceAddresses) { if (inetAddress != null && inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) { usableAddresses.add(inetAddress); } } if (usableAddresses.isEmpty()) { throw new IOException("No usable addresses found for UPnP multicast"); } InetSocketAddress localAddress = new InetSocketAddress(usableAddresses.get(0), 0); MulticastSocket ssdpSocket = new MulticastSocket(localAddress); ssdpSocket.setReuseAddress(true); logger.trace( "Sending message from multicast socket on network interface: " + ssdpSocket.getNetworkInterface()); logger.trace("Multicast socket is on interface: " + ssdpSocket.getInterface()); ssdpSocket.setTimeToLive(32); logger.trace("Socket Timeout: " + ssdpSocket.getSoTimeout()); logger.trace("Socket TTL: " + ssdpSocket.getTimeToLive()); return ssdpSocket; }