Skip to content
Snippets Groups Projects
Commit 89e436df authored by brutzman's avatar brutzman
Browse files

Merge origin/master

parents 5cbff9c0 2d1a2a50
No related branches found
No related tags found
No related merge requests found
Showing
with 463 additions and 28 deletions
package MV3500Cohort2020JulySeptember.homework3.Garibay;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
*
* @author chris
*/
public class UDPReceiverGaribay
{
// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here
public static final int RECEIVING_PORT = 1415;
public static final String DESINATION_HOST = "localhost";
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = null;
try
{
System.out.println(UdpReceiver.class.getName() + " started...");
// Create a UDP socket
udpSocket = new DatagramSocket(RECEIVING_PORT);
udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good
udpSocket.setBroadcast(false); // we're just receiving here
byte[] byteArray = new byte[1500];
DatagramPacket receivePacket = new DatagramPacket(byteArray, byteArray.length);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
DataInputStream dis = new DataInputStream(bais);
boolean isEvenParity;
int packetCount = 0;
int firstInt;
float secondFloat;
String thirdString;
String padding;
// You need a new receiving packet to read from every packet received
while (true)
{
packetCount++; // good practice to increment counter at start of loop, when possible
udpSocket.receive(receivePacket); // blocks until packet is received
// values of interest follow. order and types of what was sent must match what you are reading!
firstInt = dis.readInt(); // packetID
secondFloat = dis.readFloat();
thirdString = dis.readUTF(); // string value with guaranteed encoding, matches UTF-8 is 8 bit
isEvenParity = dis.readBoolean(); // ok, we've gotten everything we're looking for.
dis.reset(); // now clear the input stream after reading, in preparation for next loop
if (isEvenParity)
padding = " ";
else padding = "";
System.out.println("[" + UdpReceiver.class.getName() + "]" +
" packetID=" + firstInt + // have output message use same name as sender
", second float value=" + secondFloat +
", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character
" isPacketIdEvenParity=" + isEvenParity + "," + padding +
" packet counter=" + packetCount);
}
}
catch(IOException e)
{
System.err.println("Problem with UdpReceiver, see exception trace:");
System.err.println(e);
}
finally // clean up prior to exit, don't want to leave behind zombie socket
{
if (udpSocket != null)
udpSocket.close();
System.out.println(UdpReceiver.class.getName() + " complete."); // all done
}
}
}
package MV3500Cohort2020JulySeptember.homework3.Garibay;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
*
* @author chris
*/
public class UDPSenderGaribay
{
// System properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8)
// public static final int SENDING_PORT = 1414; // not needed, can let system choose an open local port
public static final int RECEIVING_PORT = 1415;
public static final int TOTAL_PACKETS_TO_SEND = 100;
// here is what we need for lab comms
// Bill Mahan's host ip
public static final String DESTINATION_HOST = "10.1.105.13"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever
@SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = null;
DataOutputStream dos = null;
int packetID = 0; // counter variable to send in packet
float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur
String message = MY_NAME + "It's not a trick question!"; // no really
String padding = new String();
try
{
System.out.println(UdpSender.class.getName() + " shows how to send simple-type values via DataOutputStream");
System.out.println(UdpSender.class.getName() + " started...");
// Create a UDP socket
udpSocket = new DatagramSocket(); // let system assign output port, then SENDING_PORT not needed
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet.
ByteArrayOutputStream baos = new ByteArrayOutputStream(1500); // how many bytes are in buffer? MTU=1500 is good
dos = new DataOutputStream(baos); // wrapper for writing values, connects both streams
// Put together a packet to send
// these types and order of variables must match on sender and receiver
byte[] byteArray = baos.toByteArray();
// ID of the host we are sending to
InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST);
// ID of the host we are sending from
InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified
DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, RECEIVING_PORT);
// Hmmm, how fast does UDP stream go? Does UDP effectively slow packets down, or does
// this cause network problems? (hint: yes for an unlimited send rate, unlike TCP).
// How do you know on the receiving side that you haven't received a
// duplicate UDP packet, out-of-order packet, or dropped packet? your responsibility.
for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill!
{
packetID++; // increment counter, prefer using explicit value to index
value = TOTAL_PACKETS_TO_SEND - packetID; // countdown
boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity
// values of interest follow. order and types of what was sent must match what you are reading!
dos.writeInt (packetID);
dos.writeFloat (value);
dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit
dos.writeBoolean(isPacketIdEvenParity);
dos.flush(); // sends DataOutputStream to ByteArrayOutputStream
byteArray = baos.toByteArray(); // OK so go get the flushed result...
datagramPacket.setData(byteArray); // and put it in the packet...
udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking.
// System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system
if (isPacketIdEvenParity)
padding = " ";
else padding = "";
Thread.sleep(1000); // Send packets at rate of one per second
System.out.println("[" + UdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress +
" sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending
}
}
catch (IOException | InterruptedException e)
{
System.err.println("Problem with UdpSender, see exception trace:");
System.err.println(e);
}
finally // clean up prior to exit, don't want to leave behind zombies
{
if (udpSocket != null)
udpSocket.close();
if (dos != null)
dos.close();
System.out.println(UdpSender.class.getName() + " complete."); // all done
}
}
}
package MV3500Cohort2020JulySeptember.homework3.Weissenberger; package MV3500Cohort2020JulySeptember.homework3.Goericke;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
...@@ -16,10 +16,9 @@ import java.net.Socket; ...@@ -16,10 +16,9 @@ import java.net.Socket;
* This class will be connected by an TCP sender over VPN (argon), calculating * This class will be connected by an TCP sender over VPN (argon), calculating
* a result of an equation and send the result back via UDP over VPN. * a result of an equation and send the result back via UDP over VPN.
* @date 08/17/2020 * @date 08/17/2020
* @author Loki * @group Goericke/Weissenberger
* @group Weissenberger/Goericke
*/ */
public class TCPNumberReceiver { public class TCPNumberReceiverUDPResultSender {
// Change this to the port where the TCP server is listening // Change this to the port where the TCP server is listening
private static final int TCP_ARGON_SERVER_PORT = 2317; private static final int TCP_ARGON_SERVER_PORT = 2317;
...@@ -34,17 +33,19 @@ public class TCPNumberReceiver { ...@@ -34,17 +33,19 @@ public class TCPNumberReceiver {
System.out.println("TCPNumberReceiver has started..."); System.out.println("TCPNumberReceiver has started...");
ServerSocket serverSocket = new ServerSocket(TCP_ARGON_SERVER_PORT); ServerSocket serverSocket = new ServerSocket(TCP_ARGON_SERVER_PORT);
InetAddress remoteAddress; InetAddress remoteAddress;
// declare the stream and readers
InputStream inputStream; InputStream inputStream;
InputStreamReader inputStreamReader; InputStreamReader inputStreamReader;
BufferedReader bufferedReader; BufferedReader bufferedReader;
// declare needed variables
String clientMessage; String clientMessage;
int number1, number2; int number1, number2;
String calculation_Method; String calculation_Method;
int iUDPResultReceivingPort; int iUDPResultReceivingPort;
// Server is up and waiting (i.e. "blocked" or paused) // Server is up and waiting (i.e. "blocked" or paused)
// Loop, infinitely, waiting for client connections. // Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while (true) { while (true) {
// block until connected to a client // block until connected to a client
...@@ -54,17 +55,21 @@ public class TCPNumberReceiver { ...@@ -54,17 +55,21 @@ public class TCPNumberReceiver {
inputStream = clientConnectionSocket.getInputStream(); inputStream = clientConnectionSocket.getInputStream();
inputStreamReader = new InputStreamReader(inputStream); inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader); bufferedReader = new BufferedReader(inputStreamReader);
// get the date from TCP packet
clientMessage = bufferedReader.readLine(); clientMessage = bufferedReader.readLine();
number1 = Integer.parseInt(bufferedReader.readLine()); number1 = Integer.parseInt(bufferedReader.readLine());
number2 = Integer.parseInt(bufferedReader.readLine()); number2 = Integer.parseInt(bufferedReader.readLine());
calculation_Method = bufferedReader.readLine(); calculation_Method = bufferedReader.readLine();
iUDPResultReceivingPort = Integer.parseInt(bufferedReader.readLine()); iUDPResultReceivingPort = Integer.parseInt(bufferedReader.readLine());
// print them out (for debugging)
System.out.println("Message recived: "+clientMessage); System.out.println("Message recived: "+clientMessage);
System.out.println("Number 1 recived: "+number1); System.out.println("Number 1 recived: "+number1);
System.out.println("Number 2 recived: "+number2); System.out.println("Number 2 recived: "+number2);
System.out.println("Calc Method recived: "+calculation_Method); System.out.println("Calc Method recived: "+calculation_Method);
System.out.println("Send result to port: "+iUDPResultReceivingPort); System.out.println("Send result to port: "+iUDPResultReceivingPort);
// This includes the port and IP numbers on both sides (the socket pair). // get the sender IP (is used for sending UDP)
remoteAddress = clientConnectionSocket.getInetAddress(); remoteAddress = clientConnectionSocket.getInetAddress();
System.out.println("Send result to IP: "+remoteAddress.getHostAddress()); System.out.println("Send result to IP: "+remoteAddress.getHostAddress());
...@@ -83,6 +88,13 @@ public class TCPNumberReceiver { ...@@ -83,6 +88,13 @@ public class TCPNumberReceiver {
} }
} }
/**
* send the result to Bernd 10x
* @param result
* @param inetAddress Bernd's IP
* @param port Bernd's UDP port number
* @throws IOException
*/
private static void sendResultViaUDP(float result, InetAddress inetAddress, int port) throws IOException{ private static void sendResultViaUDP(float result, InetAddress inetAddress, int port) throws IOException{
DatagramSocket udpSocket = null; DatagramSocket udpSocket = null;
DataOutputStream dos = null; DataOutputStream dos = null;
......
package MV3500Cohort2020JulySeptember.homework3.Mahan;
import java.io.*;
import java.net.*;
/**
*
* @author Bill
*/
public class MahanUdpReceiver
{
// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here
public static final int RECEIVING_PORT = 1415;
public static final String DESINATION_HOST = "10.1.105.12";
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = null;
try
{
System.out.println(MahanUdpReceiver.class.getName() + " started...");
// Create a UDP socket
udpSocket = new DatagramSocket(RECEIVING_PORT);
udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good
udpSocket.setBroadcast(false); // we're just receiving here
byte[] byteArray = new byte[1500];
DatagramPacket receivePacket = new DatagramPacket(byteArray, byteArray.length);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
DataInputStream dis = new DataInputStream(bais);
boolean isEvenParity;
int packetCount = 0;
int firstInt;
float secondFloat;
String thirdString;
String padding;
// You need a new receiving packet to read from every packet received
while (true)
{
packetCount++; // good practice to increment counter at start of loop, when possible
udpSocket.receive(receivePacket); // blocks until packet is received
// values of interest follow. order and types of what was sent must match what you are reading!
firstInt = dis.readInt(); // packetID
secondFloat = dis.readFloat();
thirdString = dis.readUTF(); // string value with guaranteed encoding, matches UTF-8 is 8 bit
isEvenParity = dis.readBoolean(); // ok, we've gotten everything we're looking for.
dis.reset(); // now clear the input stream after reading, in preparation for next loop
if (isEvenParity)
padding = " ";
else padding = "";
System.out.println("[" + MahanUdpReceiver.class.getName() + "]" +
" packetID=" + firstInt + // have output message use same name as sender
", second float value=" + secondFloat +
", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character
" isPacketIdEvenParity=" + isEvenParity + "," + padding +
" packet counter=" + packetCount);
}
}
catch(IOException e)
{
System.err.println("Problem with UdpReceiver, see exception trace:");
System.err.println(e);
}
finally // clean up prior to exit, don't want to leave behind zombie socket
{
if (udpSocket != null)
udpSocket.close();
System.out.println(MahanUdpReceiver.class.getName() + " complete."); // all done
}
}
}
package MV3500Cohort2020JulySeptember.homework3.Mahan;
import java.io.*;
import java.net.*;
/**
*
* @author Bill
*/
public class MahanUdpSender
{
// System properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8)
// public static final int SENDING_PORT = 1414; // not needed, can let system choose an open local port
public static final int RECEIVING_PORT = 1415;
public static final int TOTAL_PACKETS_TO_SEND = 100;
// here is what we need for lab comms
public static final String DESTINATION_HOST = "10.1.105.12"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever
@SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = null;
DataOutputStream dos = null;
int packetID = 0; // counter variable to send in packet
float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur
String message = MY_NAME + " says I see you Chris"; // no really
String padding = new String();
try
{
System.out.println(MahanUdpSender.class.getName() + " shows how to send simple-type values via DataOutputStream");
System.out.println(MahanUdpSender.class.getName() + " started...");
// Create a UDP socket
udpSocket = new DatagramSocket(); // let system assign output port, then SENDING_PORT not needed
// Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet.
ByteArrayOutputStream baos = new ByteArrayOutputStream(1500); // how many bytes are in buffer? MTU=1500 is good
dos = new DataOutputStream(baos); // wrapper for writing values, connects both streams
// Put together a packet to send
// these types and order of variables must match on sender and receiver
byte[] byteArray = baos.toByteArray();
// ID of the host we are sending to
InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST);
// ID of the host we are sending from
InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified
DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, RECEIVING_PORT);
// Hmmm, how fast does UDP stream go? Does UDP effectively slow packets down, or does
// this cause network problems? (hint: yes for an unlimited send rate, unlike TCP).
// How do you know on the receiving side that you haven't received a
// duplicate UDP packet, out-of-order packet, or dropped packet? your responsibility.
for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill!
{
packetID++; // increment counter, prefer using explicit value to index
value = TOTAL_PACKETS_TO_SEND - packetID; // countdown
boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity
// values of interest follow. order and types of what was sent must match what you are reading!
dos.writeInt (packetID);
dos.writeFloat (value);
dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit
dos.writeBoolean(isPacketIdEvenParity);
dos.flush(); // sends DataOutputStream to ByteArrayOutputStream
byteArray = baos.toByteArray(); // OK so go get the flushed result...
datagramPacket.setData(byteArray); // and put it in the packet...
udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking.
// System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system
if (isPacketIdEvenParity)
padding = " ";
else padding = "";
Thread.sleep(1000); // Send packets at rate of one per second
System.out.println("[" + MahanUdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress +
" sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending
}
}
catch (IOException | InterruptedException e)
{
System.err.println("Problem with UdpSender, see exception trace:");
System.err.println(e);
}
finally // clean up prior to exit, don't want to leave behind zombies
{
if (udpSocket != null)
udpSocket.close();
if (dos != null)
dos.close();
System.out.println(MahanUdpSender.class.getName() + " complete."); // all done
}
}
}
## Homework 2: Multicast Networking
Modify this file to describe your project work. This program uses UDP over ARGON.net
to connect with Chris Garibay's port and then
finds the modulo as being either true or false.
Just run the server and the corresponding receiver.
Also included are two screenshots to verify
send and receive capabilities.
Typical deliverables include properly packages source, execution log, and screen shots as appropriate.
References include
* [README.md](../README.md) for this homework project.
* [README.md](../../../../README.md) for course assignments execution instructions.
* [assignments source subdirectories](../../../../../assignments/src) show examples from previous cohorts.
Questions and innovation are always welcome, good luck!
assignments/src/MV3500Cohort2020JulySeptember/homework3/Mahan/receiving screenshot.png

109 KiB

assignments/src/MV3500Cohort2020JulySeptember/homework3/Mahan/sendingscreenshot.png

57.4 KiB

...@@ -11,9 +11,29 @@ References include ...@@ -11,9 +11,29 @@ References include
Questions and innovation are always welcome, good luck! Questions and innovation are always welcome, good luck!
**************************************************
Project for HW #3
Team: "Germany" (Stefan and Bernd)
**************************************************
This program(s) do(es) the following: This program(s) do(es) the following:
- send two numbers, a mathematical operator and a port number over VPN to a TCP receiver - send two numbers, a mathematical operator and a port number over VPN to a TCP receiver
- the TCP receiver calculates the result and send it back to the sender over VPN using UDP! - the TCP receiver calculates the result and send it back to the sender over VPN using UDP!
- the UDP receiver shows the result - the UDP receiver shows the result
The main focus was to send via TCP over a VPN, do something and send the result back via UDP.
How to run the project:
0. connect both computer with the argon net
1. run UDPResultReceiver on Bernd's computer
2. run TCPNumberReceiverUDPResultSender on Stefan's computer
3. find Stefan's IP within the argon net
4. change the TCP_ARGON_SERVER_IP in class TCPNumberSender to Stefan's IP.
5. run TCPNumberSender
(you can edit the numbers and run TCPNumberSender multiple times...)
File added
assignments/src/MV3500Cohort2020JulySeptember/homework3/Weissenberger/logic.jpg

35.2 KiB

File added
assignments/src/MV3500Cohort2020JulySeptember/homework3/Weissenberger/sequence.jpg

55 KiB

File added
...@@ -56,9 +56,9 @@ public class HttpWebPageSource { ...@@ -56,9 +56,9 @@ public class HttpWebPageSource {
// But a web server will write an unknown number of lines. // But a web server will write an unknown number of lines.
// So now we read until the transmisson ends. // So now we read until the transmisson ends.
InputStream is = socket.getInputStream(); InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is); Reader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
String line; String line;
int count = 0; int count = 0;
...@@ -72,8 +72,8 @@ public class HttpWebPageSource { ...@@ -72,8 +72,8 @@ public class HttpWebPageSource {
} }
catch(IOException e) catch(IOException e)
{ {
System.out.println("HttpWebPageSource: problem with client"); System.err.println("HttpWebPageSource: problem with client");
System.out.println(e); System.err.println(e);
} }
} }
} }
...@@ -22,7 +22,7 @@ public class TcpExample3Client { ...@@ -22,7 +22,7 @@ public class TcpExample3Client {
public static void main(String[] args) { public static void main(String[] args) {
// Local variables/fields // Local variables/fields
Socket socket; Socket socket = null;
InputStream is; InputStream is;
InputStreamReader isr; InputStreamReader isr;
BufferedReader br; BufferedReader br;
...@@ -72,6 +72,11 @@ public class TcpExample3Client { ...@@ -72,6 +72,11 @@ public class TcpExample3Client {
} }
finally // occurs after any other activity when shutting down finally // occurs after any other activity when shutting down
{ {
try {
if (socket != null)
socket.close();
} catch (IOException e) {}
// program exit: tell somebody about that happening. Likely cause: server drops connection. // program exit: tell somebody about that happening. Likely cause: server drops connection.
System.out.println(); System.out.println();
System.out.println("TcpExample3Client exit"); System.out.println("TcpExample3Client exit");
......
...@@ -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