List of usage examples for java.net MulticastSocket leaveGroup
public void leaveGroup(InetAddress mcastaddr) throws IOException
From source file:MulticastClient.java
public static void main(String[] args) throws IOException { MulticastSocket socket = new MulticastSocket(4446); InetAddress address = InetAddress.getByName("230.0.0.1"); socket.joinGroup(address);//from w w w.j a v a2 s . c o m DatagramPacket packet; // get a few quotes for (int i = 0; i < 5; i++) { byte[] buf = new byte[256]; packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData()); System.out.println("Quote of the Moment: " + received); } socket.leaveGroup(address); socket.close(); }
From source file:net.pms.network.UPNPHelper.java
/** * Send the UPnP BYEBYE message./*from w ww. j av a 2 s .co m*/ */ public static void sendByeBye() { logger.info("Sending BYEBYE..."); MulticastSocket multicastSocket = null; try { multicastSocket = getNewMulticastSocket(); InetAddress upnpAddress = getUPNPAddress(); multicastSocket.joinGroup(upnpAddress); sendMessage(multicastSocket, "upnp:rootdevice", BYEBYE); sendMessage(multicastSocket, "urn:schemas-upnp-org:device:MediaServer:1", BYEBYE); sendMessage(multicastSocket, "urn:schemas-upnp-org:service:ContentDirectory:1", BYEBYE); sendMessage(multicastSocket, "urn:schemas-upnp-org:service:ConnectionManager:1", BYEBYE); } catch (IOException e) { logger.debug("Error sending BYEBYE message", e); } finally { if (multicastSocket != null) { // Clean up the multicast socket nicely try { InetAddress upnpAddress = getUPNPAddress(); multicastSocket.leaveGroup(upnpAddress); } catch (IOException e) { } multicastSocket.disconnect(); multicastSocket.close(); } } }
From source file:net.pms.network.UPNPHelper.java
/** * Send alive./* www .j a v a 2 s. c om*/ */ public static void sendAlive() { logger.debug("Sending ALIVE..."); MulticastSocket multicastSocket = null; try { multicastSocket = getNewMulticastSocket(); InetAddress upnpAddress = getUPNPAddress(); multicastSocket.joinGroup(upnpAddress); sendMessage(multicastSocket, "upnp:rootdevice", ALIVE); sendMessage(multicastSocket, PMS.get().usn(), ALIVE); sendMessage(multicastSocket, "urn:schemas-upnp-org:device:MediaServer:1", ALIVE); sendMessage(multicastSocket, "urn:schemas-upnp-org:service:ContentDirectory:1", ALIVE); sendMessage(multicastSocket, "urn:schemas-upnp-org:service:ConnectionManager:1", ALIVE); } catch (IOException e) { logger.debug("Error sending ALIVE message", e); } finally { if (multicastSocket != null) { // Clean up the multicast socket nicely try { InetAddress upnpAddress = getUPNPAddress(); multicastSocket.leaveGroup(upnpAddress); } catch (IOException e) { } multicastSocket.disconnect(); multicastSocket.close(); } } }
From source file:com.all.landownloader.discovery.LanDiscoverySocket.java
public LanDiscoveredPeer listen() throws IOException, IllegalArgumentException { MulticastSocket socket = new MulticastSocket(PORT); socket.joinGroup(addressGroup);/*from www .j a v a2 s . com*/ byte[] buf = new byte[BUF_LENGTH]; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); socket.leaveGroup(addressGroup); socket.close(); InetAddress address = packet.getAddress(); byte[] remotePortBuffer = new byte[4]; System.arraycopy(buf, HEADER_LENGTH, remotePortBuffer, 0, remotePortBuffer.length); LanDiscoveredPeer discoveredPeer = new LanDiscoveredPeer(address, LanUtils.decodeInt(remotePortBuffer)); if (eq(BYE_MSG, buf, HEADER_LENGTH)) { addresses.remove(address); RegisteredAddress remove = quickLeases.remove(address); if (remove != null) { leases.remove(remove); } return null; } RegisteredAddress reg = quickLeases.get(discoveredPeer); long nextLeaseTime = System.currentTimeMillis() + this.leaseTime; if (reg == null) { reg = new RegisteredAddress(nextLeaseTime, discoveredPeer); quickLeases.put(discoveredPeer, reg); } else { reg.setLease(nextLeaseTime); } if (eq(ANNOUNCE_MSG, buf, HEADER_LENGTH)) { reply(address); } leases.add(reg); addresses.add(discoveredPeer); return discoveredPeer; }
From source file:net.pms.network.UPNPHelper.java
/** * Starts up two threads: one to broadcast UPnP ALIVE messages and another * to listen for responses. /* w ww .jav a 2 s. c om*/ * * @throws IOException Signals that an I/O exception has occurred. */ public static void listen() throws IOException { Runnable rAlive = new Runnable() { @Override public void run() { int delay = 10000; while (true) { sleep(delay); sendAlive(); // The first delay for sending an ALIVE message is 10 seconds, // the second delay is for 20 seconds. From then on, all other // delays are for 180 seconds. switch (delay) { case 10000: delay = 20000; break; case 20000: delay = 180000; break; } } } }; aliveThread = new Thread(rAlive, "UPNP-AliveMessageSender"); aliveThread.start(); Runnable r = new Runnable() { @Override public void run() { boolean bindErrorReported = false; while (true) { MulticastSocket multicastSocket = null; try { // Use configurable source port as per http://code.google.com/p/ps3mediaserver/issues/detail?id=1166 multicastSocket = new MulticastSocket(configuration.getUpnpPort()); if (bindErrorReported) { logger.warn( "Finally, acquiring port " + configuration.getUpnpPort() + " was successful!"); } NetworkInterface ni = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName(); try { // Setting the network interface will throw a SocketException on Mac OSX // with Java 1.6.0_45 or higher, but if we don't do it some Windows // configurations will not listen at all. if (ni != null) { multicastSocket.setNetworkInterface(ni); } else if (PMS.get().getServer().getNetworkInterface() != null) { multicastSocket.setNetworkInterface(PMS.get().getServer().getNetworkInterface()); logger.trace("Setting multicast network interface: " + PMS.get().getServer().getNetworkInterface()); } } catch (SocketException e) { // Not setting the network interface will work just fine on Mac OSX. } multicastSocket.setTimeToLive(4); multicastSocket.setReuseAddress(true); InetAddress upnpAddress = getUPNPAddress(); multicastSocket.joinGroup(upnpAddress); while (true) { byte[] buf = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(buf, buf.length); multicastSocket.receive(receivePacket); String s = new String(receivePacket.getData()); InetAddress address = receivePacket.getAddress(); if (s.startsWith("M-SEARCH")) { String remoteAddr = address.getHostAddress(); int remotePort = receivePacket.getPort(); if (configuration.getIpFiltering().allowed(address)) { logger.trace( "Receiving a M-SEARCH from [" + remoteAddr + ":" + remotePort + "]"); if (StringUtils.indexOf(s, "urn:schemas-upnp-org:service:ContentDirectory:1") > 0) { sendDiscover(remoteAddr, remotePort, "urn:schemas-upnp-org:service:ContentDirectory:1"); } if (StringUtils.indexOf(s, "upnp:rootdevice") > 0) { sendDiscover(remoteAddr, remotePort, "upnp:rootdevice"); } if (StringUtils.indexOf(s, "urn:schemas-upnp-org:device:MediaServer:1") > 0) { sendDiscover(remoteAddr, remotePort, "urn:schemas-upnp-org:device:MediaServer:1"); } if (StringUtils.indexOf(s, "ssdp:all") > 0) { sendDiscover(remoteAddr, remotePort, "urn:schemas-upnp-org:device:MediaServer:1"); } if (StringUtils.indexOf(s, PMS.get().usn()) > 0) { sendDiscover(remoteAddr, remotePort, PMS.get().usn()); } } } else if (s.startsWith("NOTIFY")) { String remoteAddr = address.getHostAddress(); int remotePort = receivePacket.getPort(); logger.trace("Receiving a NOTIFY from [" + remoteAddr + ":" + remotePort + "]"); } } } catch (BindException e) { if (!bindErrorReported) { logger.error("Unable to bind to " + configuration.getUpnpPort() + ", which means that PMS will not automatically appear on your renderer! " + "This usually means that another program occupies the port. Please " + "stop the other program and free up the port. " + "PMS will keep trying to bind to it...[" + e.getMessage() + "]"); } bindErrorReported = true; sleep(5000); } catch (IOException e) { logger.error("UPNP network exception", e); sleep(1000); } finally { if (multicastSocket != null) { // Clean up the multicast socket nicely try { InetAddress upnpAddress = getUPNPAddress(); multicastSocket.leaveGroup(upnpAddress); } catch (IOException e) { } multicastSocket.disconnect(); multicastSocket.close(); } } } } }; listenerThread = new Thread(r, "UPNPHelper"); listenerThread.start(); }