You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
Java
44 lines
1.1 KiB
Java
5 years ago
|
import java.io.BufferedReader;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.io.PrintStream;
|
||
|
import java.net.DatagramPacket;
|
||
|
import java.net.DatagramSocket;
|
||
|
|
||
|
public class ServeurUDP {
|
||
|
|
||
|
public static void main(String[] args) throws Exception {
|
||
|
GestionProtocole gp = new GestionProtocole();
|
||
|
// Création d'un socket UDP sur le port 40000
|
||
|
DatagramSocket socket = new DatagramSocket(40000);
|
||
|
|
||
|
// tampon pour recevoir les données des datagrammes UDP
|
||
|
final byte[] tampon = new byte[1024];
|
||
|
|
||
|
// objet Java permettant de recevoir un datagramme UDP
|
||
|
DatagramPacket dgram = new DatagramPacket(tampon, tampon.length);
|
||
|
|
||
|
while(true) {
|
||
|
// attente et réception d'un datagramme UDP
|
||
|
socket.receive(dgram);
|
||
|
|
||
|
// extraction des données
|
||
|
String chaine = new String(dgram.getData(), 0, dgram.getLength());
|
||
|
|
||
|
if (chaine != null){
|
||
|
System.out.println("requete : " + chaine);
|
||
|
|
||
|
String reponse = gp.traite(chaine);
|
||
|
}
|
||
|
|
||
|
// on renvoie le message au client
|
||
|
socket.send(dgram.setData(reponse));
|
||
|
|
||
|
// on replace la taille du tampon au max
|
||
|
// elle a été modifiée lors de la réception
|
||
|
dgram.setLength(tampon.length);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|