Created
April 8, 2019 17:28
-
-
Save nhh/ea0dd654e9d7814cca84ca2a8221d45e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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