下面列出了java.net.MulticastSocket#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create a new instance from the given {@link MulticastSocket}.
*
* @param socket the {@link MulticastSocket} which is used by this instance
*/
public OioDatagramChannel(MulticastSocket socket) {
super(null);
boolean success = false;
try {
socket.setSoTimeout(SO_TIMEOUT);
socket.setBroadcast(false);
success = true;
} catch (SocketException e) {
throw new ChannelException(
"Failed to configure the datagram socket timeout.", e);
} finally {
if (!success) {
socket.close();
}
}
this.socket = socket;
config = new DefaultOioDatagramChannelConfig(this, socket);
}
/**
* Create a new instance from the given {@link MulticastSocket}.
*
* @param socket the {@link MulticastSocket} which is used by this instance
*/
public OioDatagramChannel(MulticastSocket socket) {
super(null);
boolean success = false;
try {
socket.setSoTimeout(SO_TIMEOUT);
socket.setBroadcast(false);
success = true;
} catch (SocketException e) {
throw new ChannelException(
"Failed to configure the datagram socket timeout.", e);
} finally {
if (!success) {
socket.close();
}
}
this.socket = socket;
config = new DefaultDatagramChannelConfig(this, socket);
}
public static void closeQuietly(final MulticastSocket socket) {
if (socket == null) {
return;
}
try {
socket.close();
} catch (final Exception ex) {
logger.debug("Failed to close multicast socket due to: " + ex, ex);
}
}
void closeMulticastSocket() {
if(mcast_recv_sock != null) {
try {
if(mcast_addr != null) {
mcast_recv_sock.leaveGroup(mcast_addr.getIpAddress());
}
mcast_recv_sock.close(); // this will cause the mcast receiver thread to break out of its loop
//mcast_recv_sock=null;
if(log.isDebugEnabled()) log.debug("multicast receive socket closed");
}
catch(IOException ex) {
}
mcast_addr=null;
}
if(mcast_send_sock != null) {
mcast_send_sock.close();
//mcast_send_sock=null;
if(log.isDebugEnabled()) log.debug("multicast send socket closed");
}
if(mcast_send_sockets != null) {
MulticastSocket s;
for(int i=0; i < mcast_send_sockets.length; i++) {
s=mcast_send_sockets[i];
s.close();
if(log.isDebugEnabled()) log.debug("multicast send socket " + s + " closed");
}
mcast_send_sockets=null;
}
}
/**
* Closes the datagram socket, any multicast receive sockets,
* and interrupts their threads.
*/
@Override // GemStoneAddition
public void emergencyClose() {
closeSocket();
MulticastSocket ms = mcast_recv_sock;
if (ms != null) {
ms.close();
}
ms = mcast_send_sock;
if (ms != null) {
ms.close();
}
if (mcast_send_sockets != null) {
for (int i = 0; i < mcast_send_sockets.length; i ++) {
ms = mcast_send_sockets[i];
if (ms != null) {
ms.close();
}
}
}
Thread thr = mcast_receiver;
if (thr != null) {
thr.interrupt();
}
UcastReceiver ur = ucast_receiver;
if (ur != null) {
thr = ur.thread;
if (thr != null) {
thr.interrupt();
}
}
}
/**
* Closes the diagnostic handler, if it is open
*/
public void emergencyClose() {
DiagnosticsHandler ds = diag_handler;
if (ds != null) {
MulticastSocket ms = ds.diag_sock;
if (ms != null) {
ms.close();
}
Thread thr = ds.t;
if (thr != null) {
thr.interrupt();
}
}
}
public static void closeHard(final MulticastSocket multicastSocket) {
if (multicastSocket == null) {
return;
}
try {
multicastSocket.close();
} catch (final Exception e) {
ignoreExpectedException(e);
}
}
void closeMulticastSocket() {
if(mcast_recv_sock != null) {
try {
if(mcast_addr != null) {
mcast_recv_sock.leaveGroup(mcast_addr.getIpAddress());
}
mcast_recv_sock.close(); // this will cause the mcast receiver thread to break out of its loop
//mcast_recv_sock=null;
if(log.isDebugEnabled()) log.debug("multicast receive socket closed");
}
catch(IOException ex) {
}
mcast_addr=null;
}
if(mcast_send_sock != null) {
mcast_send_sock.close();
//mcast_send_sock=null;
if(log.isDebugEnabled()) log.debug("multicast send socket closed");
}
if(mcast_send_sockets != null) {
MulticastSocket s;
for(int i=0; i < mcast_send_sockets.length; i++) {
s=mcast_send_sockets[i];
s.close();
if(log.isDebugEnabled()) log.debug("multicast send socket " + s + " closed");
}
mcast_send_sockets=null;
}
}
/**
* Closes the datagram socket, any multicast receive sockets,
* and interrupts their threads.
*/
@Override // GemStoneAddition
public void emergencyClose() {
closeSocket();
MulticastSocket ms = mcast_recv_sock;
if (ms != null) {
ms.close();
}
ms = mcast_send_sock;
if (ms != null) {
ms.close();
}
if (mcast_send_sockets != null) {
for (int i = 0; i < mcast_send_sockets.length; i ++) {
ms = mcast_send_sockets[i];
if (ms != null) {
ms.close();
}
}
}
Thread thr = mcast_receiver;
if (thr != null) {
thr.interrupt();
}
UcastReceiver ur = ucast_receiver;
if (ur != null) {
thr = ur.thread;
if (thr != null) {
thr.interrupt();
}
}
}
/**
* Closes the diagnostic handler, if it is open
*/
public void emergencyClose() {
DiagnosticsHandler ds = diag_handler;
if (ds != null) {
MulticastSocket ms = ds.diag_sock;
if (ms != null) {
ms.close();
}
Thread thr = ds.t;
if (thr != null) {
thr.interrupt();
}
}
}
public static void destoryReceiver(MulticastSocket s) throws UnknownHostException, IOException {
if (s == null)
return;
// Leave the multicast group and close the socket
s.leaveGroup(InetAddress.getByName(GROUP));
s.close();
}
public static void destroySender(MulticastSocket s) throws IOException {
if (s == null)
return;
// When we have finished sending data close the socket
s.close();
}
public static void closeQuietly(final MulticastSocket socket) {
if (socket == null) {
return;
}
try {
socket.close();
} catch (final Exception ex) {
logger.debug("Failed to close multicast socket due to: " + ex, ex);
}
}
private static void sendNotify(String notifyMessage, InetAddress ia) throws Exception {
MulticastSocket socket = new MulticastSocket(null);
try {
socket.bind(new InetSocketAddress(PORT));
socket.setTimeToLive(4);
byte[] data = notifyMessage.toString().getBytes();
socket.send(new DatagramPacket(data, data.length, new InetSocketAddress(ia, PORT)));
} catch (Exception e) {
logger.error("sendNotify", e);
throw e;
} finally {
socket.disconnect();
socket.close();
}
}
private void close(MulticastSocket socket) throws Exception
{
socket.close();
}
private void destroy(MulticastSocketSession session) throws Exception {
MulticastSocket socket = session.getSocket();
socket.close();
}