From e1d07ddb9c5c923d68fa5f5614b9f5b0cc0a1651 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 12 Feb 2025 11:49:17 +0100 Subject: [PATCH] exercice 2 --- UDP-Scanner/.gitignore | 2 ++ UDP-Scanner/README.md | 18 ++++++++++++++++++ UDP-Scanner/src/UDPScanner.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 UDP-Scanner/.gitignore create mode 100644 UDP-Scanner/README.md create mode 100644 UDP-Scanner/src/UDPScanner.java diff --git a/UDP-Scanner/.gitignore b/UDP-Scanner/.gitignore new file mode 100644 index 0000000..d220594 --- /dev/null +++ b/UDP-Scanner/.gitignore @@ -0,0 +1,2 @@ +.vscode +bin \ No newline at end of file diff --git a/UDP-Scanner/README.md b/UDP-Scanner/README.md new file mode 100644 index 0000000..7c03a53 --- /dev/null +++ b/UDP-Scanner/README.md @@ -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). diff --git a/UDP-Scanner/src/UDPScanner.java b/UDP-Scanner/src/UDPScanner.java new file mode 100644 index 0000000..089d46c --- /dev/null +++ b/UDP-Scanner/src/UDPScanner.java @@ -0,0 +1,29 @@ +import java.net.DatagramSocket; +import java.net.SocketException; + +public class UDPScanner { + public static void main(String[] args) throws Exception { + int minPort = 1024; + int maxPort = 65535; + for (int i = minPort; i < maxPort; i++) { + if(scanPort(i)){ + System.out.println("Port " + i + " utilisé !"); + } + } + } + + /** + * Vérifie la disponibilité du port + * @param port le port à vérifier + * @return true si le port est utilisé + */ + private static boolean scanPort(int port) { + try { + DatagramSocket socket = new DatagramSocket(port); + socket.close(); + return false; + } catch (SocketException e) { + return true; + } + } +}