An echo server using UDP sockets
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpEchoServer {
static final int BUFFERSIZE = 256;
public static void main(String[] args) {
DatagramSocket sock;
DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE],
BUFFERSIZE);
try {
sock = new DatagramSocket(7);
} catch (SocketException e) {
System.out.println(e);
return;
}
// echo back everything
while (true) {
try {
sock.receive(pack);
sock.send(pack);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
}
Related examples in the same category