1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
package listengate;
import java.io.IOException;
import java.net.*;
import java.util.logging.*;
public class Main {
private static int port = 3333;
private static String ip = "127.0.0.1";
private static InetAddress hote;
private static int taille = 2048;
public static void main(String[] args) {
try {
hote = InetAddress.getByName(ip);
byte[] buf = new byte[taille];
DatagramSocket socket = new DatagramSocket(port, hote);
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while(true) {
try {
socket.receive(packet);
String chaine = new String(packet.getData(), 0, packet.getLength() );
System.out.println(chaine);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (SocketException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} |