-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.java
More file actions
142 lines (121 loc) · 4.83 KB
/
Ping.java
File metadata and controls
142 lines (121 loc) · 4.83 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package exec;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Ping {
private JFrame frame;
private JTextField ipTextField;
private JButton pingButton;
private JTable resultTable;
private DefaultTableModel tableModel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Ping();
}
});
}
public Ping() {
createAndShowGUI();
}
private void createAndShowGUI() {
frame = new JFrame("Ping GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 1));
// Textfeld zum Eingeben der IP
ipTextField = new JTextField(15);
// Button zum Ausführen des Pings
pingButton = new JButton("Ping ausführen!");
pingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ping();
}
});
// Action Listener für das JTextField, um auf Enter-Taste zu reagieren
ipTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ping();
}
});
// Tabelle zum Anzeigen der Ausgabe
tableModel = new DefaultTableModel(new Object[]{"Ping-Ergebnis"}, 0);
resultTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(resultTable);
JPanel inputPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
inputPanel.add(new JLabel("IP-Adresse: "), gbc);
gbc.gridx = 1;
inputPanel.add(ipTextField, gbc);
JPanel buttonPanel = new JPanel();
buttonPanel.add(pingButton);
frame.add(inputPanel);
frame.add(buttonPanel);
frame.add(scrollPane);
frame.setSize(400, 450);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private boolean isValidIPAddress(String address) {
try {
// Versuche, die IP-Adresse zu einem InetAddress-Objekt zu konvertieren
InetAddress inetAddress = InetAddress.getByName(address);
// Überprüfe, ob es sich um eine IPv4- oder IPv6-Adresse handelt
return inetAddress instanceof java.net.Inet4Address || inetAddress instanceof java.net.Inet6Address;
} catch (UnknownHostException e) {
// Wenn die IP-Adresse ungültig ist oder nicht gefunden werden kann
return false;
}
}
private void ping() {
String address = ipTextField.getText().trim();
StringBuffer result = new StringBuffer();
BufferedReader rdr = null;
Runtime r = Runtime.getRuntime();
try {
Process p = r.exec("ping " + address);
p.waitFor();
rdr = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()), "cp850"));
String line;
while ((line = rdr.readLine()) != null) {
if (!line.isBlank()) {
// Aufteilen des Ergebnisses und Hinzufügen jeder Zeile als separate Einträge in die Tabelle
String[] lines = line.split("\\r?\\n");
for (String splitLine : lines) {
tableModel.addRow(new Object[]{splitLine});
}
}
}
// Überprüfe den Exit-Code des Prozesses, um zu prüfen, ob das Pingen erfolgreich war
int exitCode = p.exitValue();
if (exitCode != 0) {
JOptionPane.showMessageDialog(frame, "IP-Adresse nicht gefunden.", "Fehler", JOptionPane.ERROR_MESSAGE);
} else {
// Überprüfe, ob die eingegebene IP-Adresse gültig ist
if (!isValidIPAddress(address)) {
JOptionPane.showMessageDialog(frame, "Ungültige IP-Adresse.", "Fehler", JOptionPane.ERROR_MESSAGE);
}
}
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
} finally {
try {
if (rdr != null) {
rdr.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}