exercice 2
This commit is contained in:
2
UDP-Scanner/.gitignore
vendored
Normal file
2
UDP-Scanner/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.vscode
|
||||
bin
|
||||
18
UDP-Scanner/README.md
Normal file
18
UDP-Scanner/README.md
Normal 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).
|
||||
29
UDP-Scanner/src/UDPScanner.java
Normal file
29
UDP-Scanner/src/UDPScanner.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user