Skip to content
Snippets Groups Projects
Forked from Savage / NetworkedGraphicsMV3500
3598 commits behind the upstream repository.
  • bkii's avatar
    c59383be
    Homework 4, Knobeloch · c59383be
    bkii authored
    Unable to create EntityType
    Country? Domain? Category?
    Besides that, works fine: Creates 5 new EntityStatePDUs and sends them with ONE thread. On the other side, all PDUs are received well.
    c59383be
    History
    Homework 4, Knobeloch
    bkii authored
    Unable to create EntityType
    Country? Domain? Category?
    Besides that, works fine: Creates 5 new EntityStatePDUs and sends them with ONE thread. On the other side, all PDUs are received well.
UdpSender.java 2.70 KiB
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.
 * 
 * Start this before launching UdpReceiver.
 * 
 * @author mcgredo
 * @author brutzman
 */
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("Bert 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);
        }
    }
}