Compare commits
No commits in common. "8d70aa09e7339aecd69cb74d6b6b2ee1d70abda3" and "4f6f5bb6b69818334d4617835755114e149d097a" have entirely different histories.
8d70aa09e7
...
4f6f5bb6b6
8 changed files with 62 additions and 126 deletions
|
@ -1,14 +1,9 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InterfaceAddress;
|
|
||||||
import java.net.NetworkInterface;
|
|
||||||
import java.net.SocketException;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
@ -48,7 +43,6 @@ public class ChatApp {
|
||||||
// Recuperer adresse IP de l'utilisateur
|
// Recuperer adresse IP de l'utilisateur
|
||||||
InetAddress ip = null ;
|
InetAddress ip = null ;
|
||||||
try {
|
try {
|
||||||
//ip = InetAddress.getByName("192.168.1.72");
|
|
||||||
ip = InetAddress.getLocalHost();
|
ip = InetAddress.getLocalHost();
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -76,13 +70,12 @@ public class ChatApp {
|
||||||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||||
// Message que l'on envoie à tous les utilisateurs actifs
|
// Message que l'on envoie à tous les utilisateurs actifs
|
||||||
String broadcastMessage = "Modification Pseudo\n" + this.getMe().getPseudo() + "\n" + nouveau + "\n";
|
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())))
|
if(!(p.equals(this.getMe().getPort())))
|
||||||
{
|
{
|
||||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -96,13 +89,12 @@ public class ChatApp {
|
||||||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||||
// Message que l'on envoie à tous les utilisateurs actifs
|
// Message que l'on envoie à tous les utilisateurs actifs
|
||||||
String broadcastMessage = "Connexion\n" + this.getMe().toString() ;
|
String broadcastMessage = "Connexion\n" + this.getMe().toString() ;
|
||||||
/*for(Integer p : ListPort ) {
|
for(Integer p : ListPort ) {
|
||||||
if(!(p.equals(this.getMe().getPort())))
|
if(!(p.equals(this.getMe().getPort())))
|
||||||
{
|
{
|
||||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -115,13 +107,12 @@ public class ChatApp {
|
||||||
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
InetAddress broadcastAdress = InetAddress.getLoopbackAddress(); // A MODIFIER
|
||||||
// Message que l'on envoie à tous les utilisateurs actifs
|
// Message que l'on envoie à tous les utilisateurs actifs
|
||||||
String broadcastMessage = "Deconnexion\n" + this.getMe().toString() ;
|
String broadcastMessage = "Deconnexion\n" + this.getMe().toString() ;
|
||||||
/*for(Integer p : ListPort ) {
|
for(Integer p : ListPort ) {
|
||||||
if( !(p.equals(this.getMe().getPort())))
|
if( !(p.equals(this.getMe().getPort())))
|
||||||
{
|
{
|
||||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
UDPEchange.connexion(broadcastAdress,broadcastMessage, p);
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
UDPEchange.connexion(broadcastAdress,broadcastMessage, 1024);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main (String[] args) throws IOException {
|
public static void main (String[] args) throws IOException {
|
||||||
|
@ -132,13 +123,6 @@ public class ChatApp {
|
||||||
ListPort.add(1234);
|
ListPort.add(1234);
|
||||||
ListPort.add(3000);
|
ListPort.add(3000);
|
||||||
ListPort.add(4000);
|
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);
|
ExecutorService exec = Executors.newFixedThreadPool(1000);
|
||||||
exec.submit(new Runner(app));
|
exec.submit(new Runner(app));
|
||||||
try {
|
try {
|
||||||
|
@ -192,10 +176,7 @@ public class ChatApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Runner implements Runnable {
|
class Runner implements Runnable {
|
||||||
ChatApp app ;
|
ChatApp app ;
|
||||||
public Runner(ChatApp app) {
|
public Runner(ChatApp app) {
|
||||||
|
@ -208,3 +189,4 @@ class Runner implements Runnable {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -34,7 +36,6 @@ public class MessageHorodate {
|
||||||
* @return Les differents attributs de la classe sous forme de string
|
* @return Les differents attributs de la classe sous forme de string
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String Msg = "";
|
String Msg = "";
|
||||||
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
Msg += ("Destinataire: " + this.destinataire + "\n") ;
|
||||||
|
|
Binary file not shown.
|
@ -1,4 +1,5 @@
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
Binary file not shown.
|
@ -2,12 +2,7 @@ import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.NetworkInterface;
|
|
||||||
import java.net.SocketException;
|
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.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
/**
|
/**
|
||||||
|
@ -31,24 +26,12 @@ public class UDPEchange {
|
||||||
*/
|
*/
|
||||||
public static void connexion( InetAddress broadcastAdress , String broadcastMessage , Integer port) throws IOException {
|
public static void connexion( InetAddress broadcastAdress , String broadcastMessage , Integer port) throws IOException {
|
||||||
// Envoie en broadcast à tous les utilsateurs
|
// Envoie en broadcast à tous les utilsateurs
|
||||||
for (InetAddress broadcastAddr : listAllBroadcastAddresses()) {
|
|
||||||
DatagramSocket socket = new DatagramSocket();
|
DatagramSocket socket = new DatagramSocket();
|
||||||
socket.setBroadcast(true);
|
socket.setBroadcast(true);
|
||||||
byte[]buffer = broadcastMessage.getBytes();
|
byte[]buffer = broadcastMessage.getBytes();
|
||||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, broadcastAddr, port );
|
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLoopbackAddress(), port );
|
||||||
socket.send(packet);
|
socket.send(packet);
|
||||||
socket.close();
|
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("***********Message envoye***********");
|
||||||
System.out.println("Dest Ip: " + broadcastAdress.toString());
|
System.out.println("Dest Ip: " + broadcastAdress.toString());
|
||||||
System.out.println("Dest port: " + port.toString());
|
System.out.println("Dest port: " + port.toString());
|
||||||
|
@ -106,31 +89,8 @@ public class UDPEchange {
|
||||||
System.out.println(broadcastMessage);
|
System.out.println(broadcastMessage);
|
||||||
System.out.println("************************************");
|
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>
|
* <p>
|
||||||
* Classe implémentant l'interface Runnable.
|
* Classe implémentant l'interface Runnable.
|
||||||
|
@ -281,8 +241,4 @@ class RunnerUDP implements Runnable {
|
||||||
( app.getActifUsers() ).supprimerList(Utilisateur.stringToUtilisateur(received.split("\n")[1]));
|
( app.getActifUsers() ).supprimerList(Utilisateur.stringToUtilisateur(received.split("\n")[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,6 @@ public class Utilisateur extends Object {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
public String toString(){
|
||||||
String s = "";
|
String s = "";
|
||||||
s+="pseudo " + this.pseudo + " | ";
|
s+="pseudo " + this.pseudo + " | ";
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
public class View{
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in a new issue