Skip to content

Instantly share code, notes, and snippets.

@nhh
Created April 8, 2019 17:28
Show Gist options
  • Save nhh/ea0dd654e9d7814cca84ca2a8221d45e to your computer and use it in GitHub Desktop.
Save nhh/ea0dd654e9d7814cca84ca2a8221d45e to your computer and use it in GitHub Desktop.
public class Server implements Runnable {
private DatagramSocket socket;
private boolean running;
@Override
public void run() {
running = true;
try {
socket = new DatagramSocket(4445);
System.out.println("Listening on 4445");
} catch(SocketException e) {
e.printStackTrace();
}
while (running) {
final byte[] buffer = new byte[1024];
final var request = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(request);
final var response = new DatagramPacket(request.getData(), request.getLength(), request.getAddress(), request.getPort());
socket.send(response);
} catch(IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment