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

udp example sender/receiver/log

parent 7e415cf0
No related branches found
No related tags found
No related merge requests found
package UdpMulticastHttpExamples;
import java.io.*;
import java.net.*;
/**
* An example of receiving UDP packets. Since very often both the
* sender and receiver are on the same host we use different ports
* for each. This prevents complaints from the localhost.
*
* @author mcgredo
*/
public class UdpReceiver
{
public static final int SENDING_PORT = 1414;
public static final int RECEIVING_PORT = 1415;
public static final String DESINATION_HOST = "localhost";
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
System.out.println("UdpReceiver started...");
// Create a UDP socket
DatagramSocket udpSocket = new DatagramSocket(RECEIVING_PORT);
// You need a new receiving packet to read from every packet received
while (true)
{
byte[] receiveBuffer = new byte[1500];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
udpSocket.receive(receivePacket);
// Decode the contents by extracting the data from the packet
ByteArrayInputStream bais = new ByteArrayInputStream(receivePacket.getData());
DataInputStream dis = new DataInputStream(bais);
// What happens if you read an integer? Two double values? ***
float first = dis.readFloat(); // alternatives: readFloat(); readInt(); dis.readUTF();
float second = dis.readFloat();
System.out.println("first value: " + first + " second value: " + second);
}
}
catch(IOException e)
{
System.out.println("Problem with UdpReceiver, see exception trace:");
System.out.println(e);
}
}
}
package UdpMulticastHttpExamples;
import java.io.*;
import java.net.*;
/**
* An example of sending UDP packets. The sending and receiving programs
* use different UDP ports; there can be problems getting this to work
* if both the sending and receiving sockets try to use the same port
* on the same host.
*
* @author mcgredo
*/
public class UdpSender
{
public static final int SENDING_PORT = 1414;
public static final int RECEIVING_PORT = 1415;
public static final String DESTINATION_HOST = "localhost";
@SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args)
{
try
{
System.out.println("UdpSender started...");
// Create a UDP socket
DatagramSocket udpSocket = new DatagramSocket(SENDING_PORT);
// 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();
DataOutputStream dos = new DataOutputStream(baos);
// alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\"");
dos.writeFloat(17.0f);
dos.writeFloat(24.0f);
byte[] buffer = baos.toByteArray();
// Put together a packet to send
// ID of the host we are sending to
InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, destinationAddress, RECEIVING_PORT );
// How fast does this go? Does UDP try to slow it 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?
for (int index = 1; index <= 100; index++) // avoid infinite send loops in code, they can be hard to kill!
{
udpSocket.send(packet);
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent packet " + index + " of 100");
}
System.out.println("UdpSender complete.");
}
catch (IOException | InterruptedException e)
{
System.out.println("Problem with UdpSender, see exception trace:");
System.out.println(e);
}
}
}
Invocation instructions:
1. run/debug UdpReceiver.java
2. run/debug UdpSender.java
Program responses:
===================================================
run:
UdpSender started...
Sent packet 1 of 100
Sent packet 2 of 100
Sent packet 3 of 100
Sent packet 4 of 100
Sent packet 5 of 100
Sent packet 6 of 100
Sent packet 7 of 100
Sent packet 8 of 100
Sent packet 9 of 100
Sent packet 10 of 100
Sent packet 11 of 100
Sent packet 12 of 100
Sent packet 13 of 100
Sent packet 14 of 100
Sent packet 15 of 100
Sent packet 16 of 100
Sent packet 17 of 100
Sent packet 18 of 100
Sent packet 19 of 100
Sent packet 20 of 100
Sent packet 21 of 100
Sent packet 22 of 100
Sent packet 23 of 100
Sent packet 24 of 100
Sent packet 25 of 100
Sent packet 26 of 100
Sent packet 27 of 100
Sent packet 28 of 100
Sent packet 29 of 100
Sent packet 30 of 100
Sent packet 31 of 100
Sent packet 32 of 100
Sent packet 33 of 100
Sent packet 34 of 100
Sent packet 35 of 100
Sent packet 36 of 100
Sent packet 37 of 100
Sent packet 38 of 100
Sent packet 39 of 100
Sent packet 40 of 100
Sent packet 41 of 100
Sent packet 42 of 100
Sent packet 43 of 100
Sent packet 44 of 100
Sent packet 45 of 100
Sent packet 46 of 100
Sent packet 47 of 100
Sent packet 48 of 100
Sent packet 49 of 100
Sent packet 50 of 100
Sent packet 51 of 100
Sent packet 52 of 100
Sent packet 53 of 100
Sent packet 54 of 100
Sent packet 55 of 100
Sent packet 56 of 100
Sent packet 57 of 100
Sent packet 58 of 100
Sent packet 59 of 100
Sent packet 60 of 100
Sent packet 61 of 100
Sent packet 62 of 100
Sent packet 63 of 100
Sent packet 64 of 100
Sent packet 65 of 100
Sent packet 66 of 100
Sent packet 67 of 100
Sent packet 68 of 100
Sent packet 69 of 100
Sent packet 70 of 100
Sent packet 71 of 100
Sent packet 72 of 100
Sent packet 73 of 100
Sent packet 74 of 100
Sent packet 75 of 100
Sent packet 76 of 100
Sent packet 77 of 100
Sent packet 78 of 100
Sent packet 79 of 100
Sent packet 80 of 100
Sent packet 81 of 100
Sent packet 82 of 100
Sent packet 83 of 100
Sent packet 84 of 100
Sent packet 85 of 100
Sent packet 86 of 100
Sent packet 87 of 100
Sent packet 88 of 100
Sent packet 89 of 100
Sent packet 90 of 100
Sent packet 91 of 100
Sent packet 92 of 100
Sent packet 93 of 100
Sent packet 94 of 100
Sent packet 95 of 100
Sent packet 96 of 100
Sent packet 97 of 100
Sent packet 98 of 100
Sent packet 99 of 100
Sent packet 100 of 100
UdpSender complete.
BUILD SUCCESSFUL (total time: 1 minute 40 seconds)
===================================================
run:
UdpReceiver started...
first value: 17.0 second value: 24.0
first value: 17.0 second value: 24.0
first value: 17.0 second value: 24.0
first value: 17.0 second value: 24.0
[... etc. ...]
===================================================
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