Chat, is this real?
This commit is contained in:
@@ -10,13 +10,26 @@ import java.util.Scanner;
|
|||||||
public class ChatMulticast {
|
public class ChatMulticast {
|
||||||
|
|
||||||
private static class Receiver implements Runnable {
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int port = 6969;
|
|
||||||
// Code de reception et affichage
|
// Code de reception et affichage
|
||||||
try {
|
try {
|
||||||
InetAddress groupIP = InetAddress.getByName("225.0.4.1");
|
|
||||||
MulticastSocket socket = new MulticastSocket(port);
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -24,15 +37,26 @@ public class ChatMulticast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
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
|
// 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
|
// Vous pouvez utiliser Scanner : https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html
|
||||||
while (true) {
|
Scanner sc = new Scanner(System.in);
|
||||||
byte[] buffer = new byte[1024];
|
System.out.println("Entrez ton nom (bOb):");
|
||||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
String nom = sc.nextLine();
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
|
|
||||||
|
|
||||||
|
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