Ceci est un exemple d'émetteur UDP minimaliste :
import java.net.*;
public class EmetteurUdp {
static final String DESTINATION = "127.0.0.1";
static final int PORT = 10000;
static final int DELAI = 2000;
public void emettre(String chaine) {
try {
byte tampon[] = chaine.getBytes();
InetAddress adresse = InetAddress.getByName(DESTINATION);
DatagramPacket paquet =
new DatagramPacket(tampon, 0, tampon.length, adresse, PORT);
DatagramSocket socket = new DatagramSocket();
while (true) {
socket.send(paquet);
try {
Thread.sleep(DELAI);
} catch( InterruptedException ie ) {}
}
} catch (Exception e) {
System.err.println("Houston we have a problem");
e.printStackTrace();
System.exit(1);
}
}
public static void main(String args[]) {
new EmetteurUdp().emettre("Ceci est un message");
}
}