package de.htw.vs.server;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class UDPServer extends RunnableLifeCycle {

	protected UDPServerWorker worker;
	protected int port;
	protected int timeout;


	public UDPServer(int port, UDPServerWorker worker) {
		this.port = port;
		this.timeout = 1000;
		this.worker = worker;
		setRunnable(this::doServerProcessing);
	}

	protected void doServerProcessing() {
		byte[] recvBuffer = worker.getReceiveBuffer();
		try(DatagramSocket ds = new DatagramSocket(port)) {
			ds.setSoTimeout(timeout);
			while(isRunning()) {
				DatagramPacket packet = new DatagramPacket(recvBuffer, recvBuffer.length);
				try {
					ds.receive(packet);
				} catch(SocketTimeoutException ex) {
					continue;
				}
				ds.send(worker.onReceived(packet));
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		if(port <= 1024 || port > (Short.MAX_VALUE * 2)) {
			throw new IllegalArgumentException("Default Ports(0 - 1024) are not allowed.");
		}
		this.port = port;
	}
}

@@ -19,16 +19,17 @@
 server;%0A
+%0A
 import j
@@ -116,41 +116,8 @@
 et;%0A
-import java.net.SocketException;%0A
 impo
@@ -343,32 +343,29 @@
 er) %7B%0A%09%09
-this.port = 
+setPort(
 port
+)
 ;%0A%09%09this
@@ -583,20 +583,25 @@
 mSocket(
-p
+getP
 ort
+()
 )) %7B%0A%09%09%09
@@ -870,69 +870,13 @@
 ));%0A
+
 %09%09%09%7D%0A
-%09%09%7D catch (SocketException e) %7B%0A%09%09%09e.printStackTrace();%0A
 %09%09%7D 

Add a Port validation to the UDPServer