OnJava8-Examples/network/ChatterServer.java
2016-09-23 13:23:35 -06:00

42 lines
1.3 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://OnJava8.com for more book information.
// A server that echoes datagrams
package network;
import java.net.*;
import java.io.*;
public class ChatterServer implements Runnable {
static final int INPORT = 1711;
private byte[] buf = new byte[1000];
private DatagramPacket dp =
new DatagramPacket(buf, buf.length);
public void run() {
// 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) { throw new RuntimeException(e); }
}
}