exercice 2

This commit is contained in:
2025-02-12 11:49:17 +01:00
commit e1d07ddb9c
3 changed files with 49 additions and 0 deletions

2
UDP-Scanner/.gitignore vendored Normal file
View File

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

18
UDP-Scanner/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,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;
}
}
}