exercice 4

This commit is contained in:
2025-02-12 12:35:25 +01:00
parent 0170bca3a4
commit 1f77b27748
5 changed files with 149 additions and 0 deletions

2
ClientServeur/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.vscode
bin

18
ClientServeur/README.md Normal file
View File

@@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

View File

@@ -0,0 +1,14 @@
import java.net.InetSocketAddress;
import java.util.Scanner;
public class App {
public static void main(String[] args) throws Exception {
Server server = new Server(6666);
Client client = new Client(new InetSocketAddress("localhost", 6666));
while(true) {
Scanner scan = new Scanner(System.in);
String message = scan.nextLine();
client.sendMessage(message);
}
}
}

View File

@@ -0,0 +1,62 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Random;
public class Client {
private final DatagramSocket socket;
private final InetSocketAddress address;
public Client(InetSocketAddress address) throws IOException {
this.address = address;
this.socket = new DatagramSocket();
new Thread(this::readMessages).start();
sendGreetings();
}
private void sendGreetings() throws IOException {
int random = new Random().nextInt(1000);
String machineName = "RX" + random;
sendMessage("hello serveur " + machineName);
}
public void sendMessage(String message) throws IOException {
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, this.address.getAddress(),
this.address.getPort());
this.socket.send(packet);
}
private void processHandshake(DatagramPacket packet) {
String message = new String(packet.getData(), 0, packet.getLength());
String machineName = message.split(" ")[1];
System.out.println("\t[Client] Serveur " + machineName + " ready: @" + packet.getAddress().getHostAddress() + ": "
+ packet.getPort());
}
private void processMessage(DatagramPacket packet) {
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("[Client] Message recieved : " + message);
if (message.startsWith("Serveur")) {
processHandshake(packet);
return;
}
}
private void readMessages() {
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
this.socket.receive(packet);
processMessage(packet);
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}

View File

@@ -0,0 +1,53 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Server {
private final DatagramSocket socket;
public Server(int port) throws SocketException {
this.socket = new DatagramSocket(port);
new Thread(this::readMessages).start();
}
private void sendMessage(String message, InetAddress address, int port) throws IOException {
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
this.socket.send(packet);
}
private void processHandshake(DatagramPacket packet) throws IOException {
String message = new String(packet.getData(), 0, packet.getLength());
String machineName = message.split(" ")[2];
System.out.println("\t[Server] Nouveau client : @" + packet.getAddress().getHostAddress() + ": " + packet.getPort());
sendMessage("Serveur " + machineName + " ready", packet.getAddress(), packet.getPort());
}
private void processMessage(DatagramPacket packet) throws IOException {
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("[Server] Message recieved : " + message);
if (message.startsWith("hello serveur")) {
processHandshake(packet);
return;
}
sendMessage(message, packet.getAddress(), packet.getPort());
}
private void readMessages() {
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
this.socket.receive(packet);
processMessage(packet);
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}