exemple de code java de générateur de port aléatoire
Exemple : java – obtenir les ports ouverts
importjava.io.IOException;importjava.net.Socket;publicclassApp{publicstaticvoidmain(String[] args){Socket tempSocket =newSocket();// Initializeboolean isOpened;// Loop through all the 65536 portsfor(int port =0; port <65536; port++){// We will assume the current port is opened unless// An exception occurs
isOpened =true;try{// Let's try connecting
tempSocket =newSocket("127.0.0.1", port);}catch(IOException e){// If an IOException occurs, then port is closed
isOpened =false;}finally{// Close the socket to save resources
tempSocket.close();}// If port is opened print a message to the consoleif(isOpened){String message =String.format("port %d is open", port);System.out.println(message);}}}}