Ceci est un exemple d'un récepteur UDP minimaliste :
import java.net.*;
public class RecepteurUdp {
static final int LONG_TAMPON = 1024;
static final int PORT = 10000;
public void recevoir() {
try {
byte tampon[] = new byte[LONG_TAMPON];
DatagramPacket paquet =
new DatagramPacket(tampon, 0, LONG_TAMPON);
DatagramSocket socket = new DatagramSocket(PORT);
while (true) {
socket.receive(paquet);
String chaine = new String(paquet.getData(),
paquet.getOffset(), paquet.getLength() );
System.out.println("Message: " + chaine);
System.out.println("Recu de " + paquet.getSocketAddress());
System.out.println();
}
} catch( Exception e) {
System.err.println("Houston we have a problem");
e.printStackTrace();
System.exit(1);
}
}
public static void main(String args[]) {
new RecepteurUdp().recevoir();
}
}