Skip to content
Snippets Groups Projects
Commit 6895169f authored by Brutzman, Don's avatar Brutzman, Don
Browse files

updated example program, works fine

parent 1f4b3907
No related branches found
No related tags found
No related merge requests found
...@@ -20,16 +20,15 @@ public class UdpReceiver ...@@ -20,16 +20,15 @@ public class UdpReceiver
{ {
// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here // public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here
/** socket value of shared interest */ /** socket value of shared interest */
public static final int RECEIVING_PORT = 1415; // sharable public static final int UDP_PORT = 1415; // sharable
/** socket value of shared interest */ /** socket value of shared interest */
public static final String DESTINATION_HOST = "localhost"; public static final String DESTINATION_HOST = "localhost";
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
* @param args command-line arguments * @param args command-line arguments
* @throws java.io.IOException execution error
*/ */
public static void main(String[] args) throws IOException public static void main(String[] args)
{ {
DatagramSocket udpSocket = null; DatagramSocket udpSocket = null;
...@@ -38,7 +37,7 @@ public class UdpReceiver ...@@ -38,7 +37,7 @@ public class UdpReceiver
System.out.println(UdpReceiver.class.getName() + " started..."); System.out.println(UdpReceiver.class.getName() + " started...");
// Create a UDP socket // Create a UDP socket
udpSocket = new DatagramSocket(RECEIVING_PORT); udpSocket = new DatagramSocket(UDP_PORT);
udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good
udpSocket.setBroadcast(false); // we're just receiving here udpSocket.setBroadcast(false); // we're just receiving here
// udpSocket.setReuseAddress(true); // udpSocket.setReuseAddress(true);
...@@ -62,8 +61,13 @@ public class UdpReceiver ...@@ -62,8 +61,13 @@ public class UdpReceiver
packetCount++; // good practice to increment counter at start of loop, when possible packetCount++; // good practice to increment counter at start of loop, when possible
udpSocket.receive(receivePacket); // blocks until packet is received udpSocket.receive(receivePacket); // blocks until packet is received
System.out.println("UdpReceiver address/port: " + udpSocket.getInetAddress().getHostAddress() + "/" + RECEIVING_PORT); if (packetCount == 1)
{
if (udpSocket.getInetAddress() == null)
System.out.println("UdpReceiver address/port: UDP socket address null (loopback)" + "/" + UDP_PORT);
else System.out.println("UdpReceiver address/port: " + udpSocket.getInetAddress().getHostAddress() + "/" + UDP_PORT);
}
// values of interest follow. order and types of what was sent must match what you are reading! // values of interest follow. order and types of what was sent must match what you are reading!
firstInt = dis.readInt(); // packetID firstInt = dis.readInt(); // packetID
secondFloat = dis.readFloat(); secondFloat = dis.readFloat();
...@@ -77,7 +81,8 @@ public class UdpReceiver ...@@ -77,7 +81,8 @@ public class UdpReceiver
else padding = ""; else padding = "";
System.out.println("[" + UdpReceiver.class.getName() + "]" + System.out.println("[" + UdpReceiver.class.getName() + "]" +
" packetID=" + firstInt + // have output message use same name as sender " port=" + UDP_PORT +
" packetID=" + firstInt + // have output message use same name as sender
", second float value=" + secondFloat + ", second float value=" + secondFloat +
", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character ", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character
" isPacketIdEvenParity=" + isEvenParity + "," + padding + " isPacketIdEvenParity=" + isEvenParity + "," + padding +
......
...@@ -22,7 +22,7 @@ public class UdpSender ...@@ -22,7 +22,7 @@ public class UdpSender
private static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) private 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 SENDING_PORT = 1414; // not needed, can let system choose an open local port
/** socket value of shared interest */ /** socket value of shared interest */
public static final int RECEIVING_PORT = UdpReceiver.RECEIVING_PORT; // 1415; ensure consistent public static final int UDP_PORT = UdpReceiver.UDP_PORT; // 1415; ensure consistent
private static final int TOTAL_PACKETS_TO_SEND = 100; private static final int TOTAL_PACKETS_TO_SEND = 100;
/** socket value of shared interest */ /** socket value of shared interest */
...@@ -33,10 +33,9 @@ public class UdpSender ...@@ -33,10 +33,9 @@ public class UdpSender
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
* @param args command-line arguments * @param args command-line arguments
* @throws java.io.IOException execution error
*/ */
@SuppressWarnings("SleepWhileInLoop") @SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException public static void main(String[] args)
{ {
DatagramSocket udpSocket = null; DatagramSocket udpSocket = null;
DataOutputStream dos = null; DataOutputStream dos = null;
...@@ -69,9 +68,9 @@ public class UdpSender ...@@ -69,9 +68,9 @@ public class UdpSender
// ID of the host we are sending from // ID of the host we are sending from
InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified
DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, RECEIVING_PORT); DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, UDP_PORT);
System.out.println("UdpSender address/port: " + destinationAddress.getHostAddress() + "/" + RECEIVING_PORT); System.out.println("UdpSender address/port: " + destinationAddress.getHostAddress() + "/" + UDP_PORT);
// Hmmm, how fast does UDP stream go? Does UDP effectively slow packets down, or does // 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). // this cause network problems? (hint: yes for an unlimited send rate, unlike TCP).
...@@ -100,9 +99,10 @@ public class UdpSender ...@@ -100,9 +99,10 @@ public class UdpSender
padding = " "; padding = " ";
else padding = ""; else padding = "";
Thread.sleep(1000); // Send packets at rate of one per second Thread.sleep(100); // Send packets at rate of one per second
System.out.println("[" + UdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress + System.out.println("[" + UdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress +
" sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity + " port=" + UDP_PORT +
" has sent values (" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND); ")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending baos.reset(); // clear the output stream after sending
} // end for loop } // end for loop
...@@ -116,9 +116,16 @@ public class UdpSender ...@@ -116,9 +116,16 @@ public class UdpSender
{ {
if (udpSocket != null) if (udpSocket != null)
udpSocket.close(); udpSocket.close();
try
if (dos != null) {
dos.close(); if (dos != null)
dos.close();
}
catch (IOException e)
{
System.err.println("Problem with UdpSender, see exception trace:");
System.err.println(e);
}
System.out.println(UdpSender.class.getName() + " complete."); // all done System.out.println(UdpSender.class.getName() + " complete."); // all done
} }
} }
......
examples/src/UdpMulticastExamples/UdpSenderWireshark.png

72.2 KiB

File added
This diff is collapsed.
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