Skip to content
Snippets Groups Projects
Commit 4ee5e60b authored by Terry D. Norbraten's avatar Terry D. Norbraten
Browse files

close socket

parent e774ef57
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,8 @@ package UdpMulticastExamples; ...@@ -3,6 +3,8 @@ package UdpMulticastExamples;
import edu.nps.moves.dis7.utilities.DisThreadedNetIF; import edu.nps.moves.dis7.utilities.DisThreadedNetIF;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* Looks a lot like UdpSender. Start this after launching MulticastReceiver. * Looks a lot like UdpSender. Start this after launching MulticastReceiver.
...@@ -28,11 +30,13 @@ public class MulticastSender { ...@@ -28,11 +30,13 @@ public class MulticastSender {
public static final int LOOPSIZE = 20; // 20000 public static final int LOOPSIZE = 20; // 20000
public static final String QUIT_SENTINEL = "QUIT QUIT QUIT!"; public static final String QUIT_SENTINEL = "QUIT QUIT QUIT!";
private static NetworkInterface ni;
@SuppressWarnings("SleepWhileInLoop") @SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException
{ {
MulticastSocket multicastSocket = null; MulticastSocket multicastSocket = null;
InetSocketAddress group = null;
// Put together a message with binary content. "ByteArrayOutputStream" // Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary // is a java.io utility that lets us put together an array of binary
...@@ -51,18 +55,18 @@ public class MulticastSender { ...@@ -51,18 +55,18 @@ public class MulticastSender {
System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv4Stack", "true");
// multicast group we are sending to--not a single host // multicast group we are sending to--not a single host
multicastSocket = new MulticastSocket(DESTINATION_PORT); multicastSocket = new MulticastSocket(/*DESTINATION_PORT*/);
multicastSocket.setTimeToLive(TTL); // time to live reduces scope of transmission multicastSocket.setTimeToLive(TTL); // time to live reduces scope of transmission
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT);
InetSocketAddress group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); group = new InetSocketAddress(multicastAddress, DESTINATION_PORT);
// Join group useful on receiving side // Join group useful on receiving side
multicastSocket.joinGroup(group, DisThreadedNetIF.findIpv4Interface()); multicastSocket.joinGroup(group, ni = DisThreadedNetIF.findIpv4Interface());
// You can join multiple groups here // You can join multiple groups here
byte[] buffer = baos.toByteArray(); byte[] buffer = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group/*, DESTINATION_PORT*/);
for (int index = 0; index < LOOPSIZE; index++) for (int index = 0; index < LOOPSIZE; index++)
{ {
...@@ -99,8 +103,14 @@ public class MulticastSender { ...@@ -99,8 +103,14 @@ public class MulticastSender {
System.err.println(e); System.err.println(e);
} finally { } finally {
if (multicastSocket != null) if (multicastSocket != null && !multicastSocket.isClosed()) {
try {
multicastSocket.leaveGroup(group, ni);
} catch (IOException ex) {
Logger.getLogger(MulticastSender.class.getName()).log(Level.SEVERE, null, ex);
}
multicastSocket.close(); multicastSocket.close();
}
dos.close(); dos.close();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment