bc0026c3e1
Plus many new examples in "streams" chapter.
50 lines
1.4 KiB
Java
50 lines
1.4 KiB
Java
// network/ChatterServer.java
|
|
// {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();
|
|
}
|
|
}
|