commit e1d07ddb9c5c923d68fa5f5614b9f5b0cc0a1651 Author: Persson-dev Date: Wed Feb 12 11:49:17 2025 +0100 exercice 2 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; + } + } +}