Debut implementation connexion()
This commit is contained in:
parent
09eba2d173
commit
96a81001c5
2 changed files with 115 additions and 0 deletions
73
Implementation/src/ChatApp.java
Normal file
73
Implementation/src/ChatApp.java
Normal file
|
@ -0,0 +1,73 @@
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ChatApp {
|
||||
|
||||
/* Liste des utilisateurs actifs */
|
||||
private ArrayList<Utilisateur> actifUsers ;
|
||||
|
||||
/* ChatApp est associé à un utilisateur */
|
||||
private Utilisateur me;
|
||||
|
||||
public ChatApp(String pseudo, Integer port){
|
||||
this.actifUsers = new ArrayList<Utilisateur>() ;
|
||||
// Recuperer adresse IP de l'utilisateur
|
||||
InetAddress ip = null ;
|
||||
try {
|
||||
ip = InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//ip.getHostAddress();
|
||||
this.me = new Utilisateur(pseudo,port,ip);
|
||||
this.actifUsers.add(me);
|
||||
}
|
||||
|
||||
public void connexion() throws IOException {
|
||||
// Envoie en broadcast à tous les utilsateurs
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
socket.setBroadcast(true);
|
||||
// @ de broadcast du réseau de l'utilisateur me
|
||||
InetAddress broadcastAdress = this.me.getIp(); // A MODIFIER
|
||||
// Message que l'on envoie à tous les utilisateurs actifs
|
||||
String broadcastMessage = this.me.toString() ;
|
||||
byte[]buffer = broadcastMessage.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket( buffer, buffer.length, InetAddress.getLoopbackAddress(), 1234 );
|
||||
socket.send(packet);
|
||||
socket.close();
|
||||
System.out.println("Chat app -> " + broadcastMessage);
|
||||
}
|
||||
|
||||
public static void main (String[] args) {
|
||||
//Integer p = 2345 ;
|
||||
ChatApp app = new ChatApp(args[0],Integer.parseInt(args[1])) ;
|
||||
try {
|
||||
app.connexion();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
DatagramSocket socket = null;
|
||||
try {
|
||||
socket = new DatagramSocket(1234);
|
||||
} catch (SocketException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
byte buffer[] = new byte[1024];
|
||||
while(true)
|
||||
{
|
||||
DatagramPacket data = new DatagramPacket(buffer,buffer.length);
|
||||
try {
|
||||
socket.receive(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
42
Implementation/src/Utilisateur.java
Normal file
42
Implementation/src/Utilisateur.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
import java.net.InetAddress;
|
||||
|
||||
public class Utilisateur {
|
||||
|
||||
private String pseudo ;
|
||||
private Integer port;
|
||||
private InetAddress ip ;
|
||||
public Utilisateur(String pseudo,Integer port, InetAddress ip ){
|
||||
this.setPseudo(pseudo) ;
|
||||
this.setPort(port);
|
||||
this.ip = ip ;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
public void setPseudo(String pseudo) {
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public InetAddress getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
String s = "";
|
||||
s+="pseudo " + this.pseudo + " \n";
|
||||
s+="port " + (this.port).toString() + "\n";
|
||||
s+="id " + (this.ip).toString() + "\n";
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue