OnJava8-Examples/network/ChatterServer.java
2016-09-03 12:17:25 -06:00

50 lines
1.5 KiB
Java

// network/ChatterServer.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ValidateByHand}
// A server that echoes datagrams
import java.net.*;
import java.io.*;
import onjava.*;
public class ChatterServer {
static final int INPORT = 1711;
private byte[] buf = new byte[1000];
private DatagramPacket dp =
new DatagramPacket(buf, buf.length);
public ChatterServer() {
// Can listen & send on the same socket:
try (
DatagramSocket socket = new DatagramSocket(INPORT)
) {
System.out.println("Server started");
while(true) {
// Block until a datagram appears:
socket.receive(dp);
String rcvd = Dgram.toString(dp) +
", from address: " + dp.getAddress() +
", port: " + dp.getPort();
System.out.println(rcvd);
String echoString =
"Echoed: " + rcvd;
// Extract the address and port from the
// received datagram to find out where to
// send it back to:
DatagramPacket echo =
Dgram.toDatagram(echoString,
dp.getAddress(), dp.getPort());
socket.send(echo);
}
} catch(IOException e) {
System.out.println("Communication error");
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
new TimedAbort(5); // Terminate after 5 seconds
new ChatterServer();
}
}