/** * Sends a leave message to the multicast group and leaves it. The <CODE>DiscoveryResponder</CODE> * leaves its multicast group. This method has no effect if the <CODE>DiscoveryResponder</CODE> is * <CODE>OFFLINE</CODE> or <CODE>STOPPING</CODE> or <CODE>STARTING</CODE>. */ public void stop() { if (state == ONLINE) { changeState(STOPPING); // ------------------------ // Stop corresponding thread // ------------------------ responder.stopRequested = true; synchronized (responder.interrupted) { if (!responder.interrupted.booleanValue()) { responderThread.interrupt(); } } // Fix for cases when the interrupt does not work (Windows NT) try { MulticastSocket ms = new MulticastSocket(multicastPort); // NPCTE fix for bugId 4499338, esc 0, 04 Sept 2001 if (usrInet != null) { ms.setInterface(usrInet); if (logger.finerOn()) { logger.finer("stop ", "use the interface " + usrInet); } } // end of NPCTE fix for bugId 4499338 InetAddress group = InetAddress.getByName(multicastGroup); ms.joinGroup(group); ms.send(new DatagramPacket(new byte[1], 1, group, multicastPort)); ms.leaveGroup(group); } catch (Exception e) { if (logger.finerOn()) { logger.finer( "stop ", "Unexpected exception occurred trying to send empty message " + e.getMessage()); } } // ------------------------ // free 'remained' allocated resource // ------------------------ responder = null; System.runFinalization(); // ---------------- // Update state // ---------------- // changeState(OFFLINE) ; } else { if (logger.finerOn()) { logger.finer("stop ", "Responder is not ONLINE"); } } }
public boolean close() { if (ssdpMultiSock == null) return true; try { ssdpMultiSock.leaveGroup(ssdpMultiGroup, ssdpMultiIf); ssdpMultiSock = null; } catch (Exception e) { // Debug.warning(e); return false; } return true; }
public void destory() { procMsgThd.stop0(); procMsgThd.interrupt(); mainThread.interrupt(); if (group != null) { try { if (recvSocket instanceof MulticastSocket) { ((MulticastSocket) recvSocket).leaveGroup(group); } } catch (IOException ioe) { } } sendSocket.close(); recvSocket.close(); }
void closeMulticastSocket() { if (mcast_sock != null) { try { if (mcast_addr != null) { // by sending a dummy packet the thread will be awakened sendDummyPacket(mcast_addr.getIpAddress(), mcast_addr.getPort()); Util.sleep(300); mcast_sock.leaveGroup(mcast_addr.getIpAddress()); } mcast_sock.close(); // this will cause the mcast receiver thread to break out of its loop mcast_sock = null; if (Trace.trace) { Trace.info("UDP.closeMulticastSocket()", "multicast socket closed"); } } catch (IOException ex) { } mcast_addr = null; } }
public static void main(String[] args) throws IOException { MulticastSocket socket = new MulticastSocket(4446); InetAddress address = InetAddress.getByName("230.0.0.1"); socket.joinGroup(address); 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(), 0, packet.getLength()); System.out.println("Quote of the Moment: " + received); } socket.leaveGroup(address); socket.close(); }