Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

import java.net.

MulticastSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

class Multicasted {
private static final String SECRET_KEY = "SecretKey123"; // Secret key for
encryption

public static void main(String[] arg) throws Exception {


System.out.println("Votre nom: ");
Scanner scan = new Scanner(System.in);
String name = scan.next();

InetAddress ipgroup = InetAddress.getByName("224.0.0.200");


int port = 6000;

// Initialize message history


ArrayList<String> messageHistory = new ArrayList<>();

// Initialize user credentials


Map<String, String> userCredentials = new HashMap<>();
userCredentials.put("user1", "password1");
userCredentials.put("user2", "password2");

new Sender(name, ipgroup, port, SECRET_KEY, messageHistory);


new Receiver(name, ipgroup, port, SECRET_KEY, messageHistory,
userCredentials);
}

static class Sender extends Thread {


InetAddress ipgroup;
int port;
MulticastSocket socket_s;
String name;
String secretKey;
ArrayList<String> messageHistory;

Sender(String name, InetAddress ipgroup, int port, String secretKey,


ArrayList<String> messageHistory) throws Exception {
this.ipgroup = ipgroup;
this.port = port;
this.name = name;
this.secretKey = secretKey;
this.messageHistory = messageHistory;
socket_s = new MulticastSocket();
start();
}

public void run() {


BufferedReader red;
try {
red = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String input = red.readLine();
if (input.startsWith("/quit")) {
System.out.println("Exiting...");
break;
}
sendMessage(input);
}
} catch (Exception ex) {
System.out.println(ex);
}
}

void sendMessage(String text) throws Exception {


byte[] msgByte;
DatagramPacket message;

ByteArrayOutputStream output = new ByteArrayOutputStream();


text = name + " : " + text;
(new DataOutputStream(output)).writeUTF(text);
msgByte = output.toByteArray();

// Encrypt message
byte[] encryptedMsg = encrypt(msgByte, secretKey);

message = new DatagramPacket(encryptedMsg, encryptedMsg.length,


ipgroup, port);
socket_s.send(message);

// Add message to history


messageHistory.add(text);
}

// Encryption method
byte[] encrypt(byte[] data, String secretKey) throws Exception {
// Your encryption logic here
return data;
}
}

static class Receiver extends Thread {


InetAddress ipgroup;
int port;
String name;
MulticastSocket socket_r;
String secretKey;
ArrayList<String> messageHistory;
Map<String, String> userCredentials;

Receiver(String name, InetAddress ipgroup, int port, String secretKey,


ArrayList<String> messageHistory, Map<String, String> userCredentials) throws
Exception {
this.ipgroup = ipgroup;
this.port = port;
this.name = name;
this.secretKey = secretKey;
this.messageHistory = messageHistory;
this.userCredentials = userCredentials;
socket_r = new MulticastSocket(port);
socket_r.joinGroup(ipgroup);
start();
}

public void run() {


DatagramPacket message;
byte[] msgByte;
String text;

while (true) {
msgByte = new byte[1024];
message = new DatagramPacket(msgByte, msgByte.length);
try {
socket_r.receive(message);
// Decrypt message
byte[] decryptedMsg = decrypt(message.getData(), secretKey);
text = (new DataInputStream(new
ByteArrayInputStream(decryptedMsg))).readUTF();
System.out.println(text);
messageHistory.add(text);
// Introduce a delay to avoid blocking
Thread.sleep(100);
} catch (Exception exc) {
System.out.println(exc);
}
}
}

// Decryption method
byte[] decrypt(byte[] data, String secretKey) throws Exception {
// Your decryption logic here
return data;
}

// Authentication method
boolean authenticateUser(String username, String password) {
return userCredentials.containsKey(username) &&
userCredentials.get(username).equals(password);
}
}
}

You might also like