Chat, is this real?
This commit is contained in:
@@ -10,13 +10,26 @@ import java.util.Scanner;
|
||||
public class ChatMulticast {
|
||||
|
||||
private static class Receiver implements Runnable {
|
||||
InetAddress group;
|
||||
int port;
|
||||
public Receiver(String s, int p) throws UnknownHostException {
|
||||
this.group = InetAddress.getByName(s);
|
||||
this.port = p;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
int port = 6969;
|
||||
|
||||
// Code de reception et affichage
|
||||
try {
|
||||
InetAddress groupIP = InetAddress.getByName("225.0.4.1");
|
||||
MulticastSocket socket = new MulticastSocket(port);
|
||||
//socket.joinGroup(group);
|
||||
while(true){
|
||||
byte[] buffer = new byte[1024];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(packet);
|
||||
String mess = new String(packet.getData());
|
||||
System.out.println(mess);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -24,15 +37,26 @@ public class ChatMulticast {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String groupeIP = "225.0.4.1";
|
||||
int port = 6969;
|
||||
|
||||
Thread receiver = new Thread(new Receiver(groupeIP, port));
|
||||
receiver.start();
|
||||
|
||||
// Lecture au clavier et envoie du message
|
||||
// Vous pouvez utiliser Scanner : https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html
|
||||
while (true) {
|
||||
byte[] buffer = new byte[1024];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Entrez ton nom (bOb):");
|
||||
String nom = sc.nextLine();
|
||||
|
||||
while(true){
|
||||
String mess = sc.nextLine();
|
||||
String nom_et_mess = nom + "> " + mess;
|
||||
System.out.println(nom_et_mess);
|
||||
byte[] buffer = nom_et_mess.getBytes();
|
||||
DatagramPacket datagramPacket= new DatagramPacket(buffer, buffer.length, InetAddress.getByName(groupeIP), port);
|
||||
MulticastSocket socketEmission = new MulticastSocket(port);
|
||||
socketEmission.send(datagramPacket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user