OnJava8-Examples/network/ChatterServer.java
2015-11-14 16:18:05 -08:00

51 lines
1.5 KiB
Java

// network/ChatterServer.java
// ©2016 MindView LLC: see Copyright.txt
// {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);
// Can listen & send on the same socket:
private DatagramSocket socket;
public ChatterServer() {
try {
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:
DatagramPacket echo =
Dgram.toDatagram(echoString,
dp.getAddress(), dp.getPort());
socket.send(echo);
}
} catch(SocketException e) {
System.err.println("Can't open socket");
System.exit(1);
} 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();
}
}