Salut à tous
j'ai créé une classe qui me permet de scanner les ports de mon PC ( ouverts ou fermés)... elle fonctionne mais je crois qu'elle a quelques défauts, par exemple mes ports sont toujours fermées or ce n'est pas le cas puisque le port 8080 utilisé pour afficher des pages web est ouvert normalement :s
De plus, je souhaiterai ajouter cette classe dans une petite interface graphique : quand je clique sur le bouton scan, je voudrai qu'il m'affiche la reponse dans un panel avec une bar de progression ..
merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
package app;
 
/**
 *
 * @author Nahla
 */
import java.net.*;
import java.io.*;
 
public class Main
{
	static int startPort = -1, endPort = -1;
 
	static void scan(String name)
	{
		try
		{
 
			InetAddress host = InetAddress.getByName(name);
			System.out.println("Host Name: " + host.getHostName());
			System.out.println("IP Address: " + toText(host.getAddress()));
 
			if (endPort < startPort) {
				int tmpPort = endPort;
				endPort = startPort;
				startPort = tmpPort;
			}
 
			for(int testPort = startPort; testPort <= endPort; testPort++)
			{
				try
				{
					Socket socket = new Socket(host, testPort);
					System.out.println("Port: " + testPort + " is open");
					socket.close();
				}	catch(IOException ex)	{
					System.out.println("Port " + testPort + " is closed");
				}
			}
 
		}	catch(UnknownHostException ex)	{
			System.out.println("Error: No such host");
		}
	}
 
	static String toText(byte ip[])
	{
		StringBuffer result = new StringBuffer();
		for(int i = 0; i < ip.length; i++)
		{
			if(i > 0)
				result.append(".");
			result.append(0xff & ip[i]);
		}
		return result.toString();
	}
 
 
	public static void main(String args[])
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 
		try
		{
			System.out.print("Enter Host Name: ");
			String name = in.readLine();
			System.out.print("Enter starting port: ");
			try {
				startPort = Integer.parseInt(in.readLine());
			} catch (Exception ex) {
				startPort = -1;
			}
			System.out.print("Enter ending port: ");
			try {
				endPort = Integer.parseInt(in.readLine());
			} catch (Exception ex) {
				endPort = -1;
			}
			System.out.flush();
			if ((startPort != -1) && (endPort != -1)) {
				scan(name);
			} else {
				System.out.println("Invalid ports");
			}
		}	catch(IOException ex)	{
			System.out.println("Error");
		}
	}
}