Ajout de la prise en compte de l'adresse de broadcast
This commit is contained in:
parent
09b6dc8525
commit
94100fad64
6 changed files with 85 additions and 18 deletions
Binary file not shown.
|
@ -1,7 +1,14 @@
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
|
@ -41,6 +48,7 @@ public class ChatApp {
|
|||
// Recuperer adresse IP de l'utilisateur
|
||||
InetAddress ip = null ;
|
||||
try {
|
||||
//ip = InetAddress.getByName("192.168.1.72");
|
||||
ip = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -63,12 +71,13 @@ public class ChatApp {
|
|||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = "Modification Pseudo\n" + this.getMe().getPseudo() + "\n" + nouveau + "\n";
|
||||
for(Integer p : ListPort ) {
|
||||
/*for(Integer p : ListPort ) {
|
||||
if(!(p.equals(this.getMe().getPort())))
|
||||
{
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
||||
}
|
||||
|
||||
|
||||
|
@ -82,12 +91,13 @@ public class ChatApp {
|
|||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = "Connexion\n" + this.getMe().toString() ;
|
||||
for(Integer p : ListPort ) {
|
||||
/*for(Integer p : ListPort ) {
|
||||
if(!(p.equals(this.getMe().getPort())))
|
||||
{
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,12 +110,13 @@ public class ChatApp {
|
|||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = "Deconnexion\n" + this.getMe().toString() ;
|
||||
for(Integer p : ListPort ) {
|
||||
/*for(Integer p : ListPort ) {
|
||||
if(!(p.equals(this.getMe().getPort())))
|
||||
{
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
||||
}
|
||||
|
||||
public static void main (String[] args) throws IOException {
|
||||
|
@ -114,6 +125,13 @@ public class ChatApp {
|
|||
ListPort.add(1234);
|
||||
ListPort.add(3000);
|
||||
ListPort.add(4000);
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
System.out.println("Mon adresse:" + localHost.toString());
|
||||
/*for(InetAddress broadcastAddr : UDPEchange.listAllBroadcastAddresses()) {
|
||||
System.out.println("Broadcast sent with address " + broadcastAddr.toString());
|
||||
}*/
|
||||
//InetAddress broadcastAddress = InterfaceAddress.getBroadcast();
|
||||
//System.out.println("Mon adresse:" + localHost.toString());
|
||||
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
||||
exec.submit(new Runner(app));
|
||||
try {
|
||||
|
@ -151,7 +169,10 @@ public class ChatApp {
|
|||
return actifUsers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class Runner implements Runnable {
|
||||
ChatApp app ;
|
||||
public Runner(ChatApp app) {
|
||||
|
@ -165,3 +186,5 @@ class Runner implements Runnable {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -2,7 +2,12 @@ import java.io.IOException;
|
|||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
/**
|
||||
|
@ -26,12 +31,24 @@ public class UDPEchange {
|
|||
*/
|
||||
public static void connexion( InetAddress broadcastAdress , String broadcastMessage , Integer port) throws IOException {
|
||||
// Envoie en broadcast à tous les utilsateurs
|
||||
for (InetAddress broadcastAddr : listAllBroadcastAddresses()) {
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLoopbackAddress(), port );
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, broadcastAddr, port );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
System.out.println("Broadcast sent with address " + broadcastAddr.toString());
|
||||
}
|
||||
/*
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, broadcastAdress, port );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
*/
|
||||
|
||||
System.out.println("***********Message envoye***********");
|
||||
System.out.println("Dest Ip: " + broadcastAdress.toString());
|
||||
System.out.println("Dest port: " + port.toString());
|
||||
|
@ -89,8 +106,31 @@ public class UDPEchange {
|
|||
System.out.println(broadcastMessage);
|
||||
System.out.println("************************************");
|
||||
}
|
||||
|
||||
|
||||
static List<InetAddress> listAllBroadcastAddresses() throws SocketException {
|
||||
List<InetAddress> broadcastList = new ArrayList<>();
|
||||
Enumeration<NetworkInterface> interfaces
|
||||
= NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = interfaces.nextElement();
|
||||
|
||||
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
networkInterface.getInterfaceAddresses().stream()
|
||||
.map(a -> a.getBroadcast())
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(broadcastList::add);
|
||||
}
|
||||
return broadcastList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Classe implémentant l'interface Runnable.
|
||||
|
@ -241,4 +281,8 @@ class RunnerUDP implements Runnable {
|
|||
( app.getActifUsers() ).supprimerList(Utilisateur.stringToUtilisateur(received.split("\n")[1]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue