This commit is contained in:
2026-02-04 08:36:08 +01:00
commit 623f5e168e
5 changed files with 146 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
bin
lib
.vscode

18
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).

11
src/Ex1/App.java Normal file
View File

@@ -0,0 +1,11 @@
package Ex1;
import java.net.InetSocketAddress;
public class App {
public static void main(String[] args) throws Exception {
Server server = new Server(6666);
Client client = new Client(new InetSocketAddress("localhost", 6666));
Client client2 = new Client(new InetSocketAddress("localhost", 6666));
}
}

65
src/Ex1/Client.java Normal file
View File

@@ -0,0 +1,65 @@
package Ex1;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Random;
import java.util.Scanner;
public class Client {
private final DatagramSocket socket;
private final InetSocketAddress address;
/**
* Create a new client
*
* @param address server adress (needed
* @throws IOException
*/
public Client(InetSocketAddress address) throws IOException {
this.address = address;
this.socket = new DatagramSocket();
sendGreetings();
readMessage();
this.socket.close();
}
private void sendGreetings() throws IOException {
sendMessage("Bonjour, je suis un client");
}
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 processMessage(DatagramPacket packet) {
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("[Client] Message recieved : " + message);
}
private boolean readMessage() {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
this.socket.receive(packet);
processMessage(packet);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private void readMessages() {
while (true) {
if(!readMessage())
break;
}
}
}

49
src/Ex1/Server.java Normal file
View File

@@ -0,0 +1,49 @@
package Ex1;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.time.Instant;
public class Server {
private final DatagramSocket socket;
public Server(int port) throws SocketException {
this.socket = new DatagramSocket(port);
new Thread(this::readMessages).start();
}
public int getPort() {
return this.socket.getLocalPort();
}
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 processMessage(DatagramPacket packet) throws IOException {
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("[Server] Message recieved : " + message);
sendMessage("Bonjour, il est " + Instant.now(), 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;
}
}
}
}