Reading Packets - Valkryst/VUDP GitHub Wiki

With a PacketReader

package com.valkryst.VUDP;

import java.net.DatagramSocket;
import java.net.SocketException;

public class Driver {
    public static void main(String[] args) throws SocketException {
        // Create a DatagramSocket
        final int portToListenOn = 1234;
        final DatagramSocket socket = new DatagramSocket(portToListenOn);

        // Create a PacketReader
        final PacketReader reader = new PacketReader(socket);
        reader.start();

        // Continually Read Packets
        while (true) {
            try {
                reader.dequeuePacket();

                // todo Do something with the packet.
            } catch (final InterruptedException e) {
                // If interrupted while waiting to take a packet from the queue.
                e.printStackTrace();
            }
        }
    }
}

With a PacketReadWriter

package com.valkryst.VUDP;

import java.net.SocketException;
import java.net.UnknownHostException;

public class Driver {
    public static void main(String[] args) throws SocketException, UnknownHostException {
        // Create a PacketReadWriter
        final String serverHost = "server.com";
        final int serverPort = 1234;
        final int clientPort = 1234;
        final PacketReadWriter readWriter = new PacketReadWriter("server-hostname.com", serverPort, clientPort);
        readWriter.start();

        // Continually Read Packets
        while (true) {
            try {
                readWriter.dequeuePacket();

                // todo Do something with the packet.
            } catch (final InterruptedException e) {
                // If interrupted while waiting to take a packet from the queue.
                e.printStackTrace();
            }
        }
    }
}