UDP (en cours)
This commit is contained in:
parent
b1ac3037b2
commit
4391f2c419
4 changed files with 97 additions and 6 deletions
|
@ -2,12 +2,11 @@ package nom;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import TypeListeUtilisateur.java;
|
||||
|
||||
public class GestionnaireListeUtilisateur {
|
||||
|
||||
//contient la liste Utilisateur à jour
|
||||
public List<TypeListeUtilisateur> listeUtilisateur = new ArrayList<TypeListeUtilisateur>(); //créer un nouveau type : nom = (String[], int) ?
|
||||
public List<TypeListeUtilisateur> listeUtilisateur = new ArrayList<TypeListeUtilisateur>();
|
||||
|
||||
//Instance du gestionnaire de liste
|
||||
static private GestionnaireListeUtilisateur uniqueInstance = null;
|
||||
|
@ -20,7 +19,7 @@ public class GestionnaireListeUtilisateur {
|
|||
GestionnaireListeUtilisateur.uniqueInstance = new GestionnaireListeUtilisateur();
|
||||
}
|
||||
|
||||
return GestionnaireListeUtilisateur.uniqueInstance
|
||||
return GestionnaireListeUtilisateur.uniqueInstance;
|
||||
|
||||
}
|
||||
|
||||
|
@ -29,6 +28,17 @@ public class GestionnaireListeUtilisateur {
|
|||
|
||||
}
|
||||
|
||||
|
||||
//ecoute le port de broadcast pour recevoir le message personnalisé
|
||||
public String ecoute() {
|
||||
|
||||
|
||||
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
//met à jour la Liste d'utilisateur
|
||||
public void MAJListeUtilisateur() {
|
||||
ajouteUtilisateur();
|
||||
|
|
|
@ -2,7 +2,7 @@ package nom;
|
|||
|
||||
public class TypeListeUtilisateur {
|
||||
|
||||
public String[] nom;
|
||||
public int id;
|
||||
public String[] IP;
|
||||
public String nom;
|
||||
public String id;
|
||||
public String IP;
|
||||
}
|
||||
|
|
34
Projet_POO/src/nom/UdpUnicastClient.java
Normal file
34
Projet_POO/src/nom/UdpUnicastClient.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package nom;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UdpUnicastClient implements Runnable{
|
||||
|
||||
private final int port;
|
||||
|
||||
|
||||
public UdpUnicastClient(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
|
||||
public void run() {
|
||||
try(DatagramSocket clientSocket = new DatagramSocket(7000)){
|
||||
byte[] buffer = new byte[65507];
|
||||
clientSocket.setSoTimeout(3000);
|
||||
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
47
Projet_POO/src/nom/UdpUnicastServer.java
Normal file
47
Projet_POO/src/nom/UdpUnicastServer.java
Normal file
|
@ -0,0 +1,47 @@
|
|||
package nom;
|
||||
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UdpUnicastServer implements Runnable{
|
||||
|
||||
private final int clientPort;
|
||||
private final String clientIP;
|
||||
|
||||
public UdpUnicastServer(int clientPort, String clientIP) {
|
||||
this.clientPort = clientPort;
|
||||
this.clientIP = clientIP;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void run() {
|
||||
try(DatagramSocket serverSocket = new DatagramSocket(7000)){
|
||||
for (int i = 0; i<3; i++) { //à remplacer par un while plus tard ?
|
||||
String message = "Message number " + i;
|
||||
DatagramPacket datagramPacket = new DatagramPacket(
|
||||
message.getBytes(),
|
||||
message.length(),
|
||||
InetAddress.getByName(clientIP),
|
||||
clientPort
|
||||
);
|
||||
serverSocket.send(datagramPacket);
|
||||
}
|
||||
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue