我正在尝试查看某些服务器是否在IP上的一系列端口上联机。由于某种原因,它总是说离线。我在另一个应用程序中尝试了ping方法,它运行完美。我正在制作的这个应用程序是多线程的,但我不知道这是否真的影响了ping。这是我正在做的所有主要课程
public class Tools extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public static String ip;
public static int port;
public static int startPort;
public static int lastPort;
public static JList<String> list;
public static JList<String> list_1;
public static JList<String> list_2;
public static DefaultListModel<String> model = new DefaultListModel<>();
public static DefaultListModel<String> model_1 = new DefaultListModel<>();
public static DefaultListModel<String> model_2 = new DefaultListModel<>();
public static Thread[] threads = new Thread[256];
private JButton btnStopBeforeIt;
private JTextField textField_1;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tools frame = new Tools();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Tools() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 846, 694);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(20, 11, 369, 50);
contentPane.add(textField);
textField.setColumns(10);
JButton btnStart = new JButton("Start");
btnStart.setBounds(399, 11, 89, 50);
contentPane.add(btnStart);
list = new JList<String>();
list.setBounds(552, 88, 256, 557);
contentPane.add(list);
list_1 = new JList<String>();
list_1.setBounds(20, 88, 256, 557);
contentPane.add(list_1);
list_2 = new JList<String>();
list_2.setBounds(286, 88, 256, 557);
contentPane.add(list_2);
btnStopBeforeIt = new JButton("STOP BEFORE IT CRASHES UR COMPUTER");
btnStopBeforeIt.setForeground(Color.RED);
btnStopBeforeIt.setBounds(498, 0, 310, 31);
contentPane.add(btnStopBeforeIt);
textField_1 = new JTextField();
textField_1.setBounds(498, 42, 86, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(722, 42, 86, 20);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblRanges = new JLabel("Ranges");
lblRanges.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblRanges.setBounds(622, 42, 68, 19);
contentPane.add(lblRanges);
JLabel lblStart = new JLabel("Start");
lblStart.setBounds(527, 63, 46, 14);
contentPane.add(lblStart);
JLabel lblEnd = new JLabel("End");
lblEnd.setBounds(757, 63, 46, 14);
contentPane.add(lblEnd);
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!textField.getText().equalsIgnoreCase("")){
startPort = Integer.parseInt(textField_1.getText());
lastPort = Integer.parseInt(textField_2.getText());
port = startPort;
ip = textField.getText();
loadThreads();
startThreads();
}else{
JOptionPane.showMessageDialog(null,"The IP address textbox cannot be left blank!", "Derpius Tools", JOptionPane.OK_OPTION);
}
}
});
btnStopBeforeIt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopThreads();
}
});
}
public static void loadThreads(){
for(int i = 0; i != 256; i++){
threads[i] = new Thread(new Pinger());
}
}
public static void startThreads(){
for(int i = 0; i != 256; i++){
threads[i].start();
}
}
@SuppressWarnings("deprecation")
public static void stopThreads(){
for(int i = 0; i != 256; i++){
threads[i].stop();
}
}
}
这是我的Pinger课程:
class Pinger implements Runnable{
@Override
public void run() {
if(serverIsOnline()){
Tools.model.addElement(Tools.ip);
Tools.model_1.addElement("Online!");
Tools.model_2.addElement(String.valueOf(Tools.port));
Tools.list.setModel(Tools.model);
Tools.list_1.setModel(Tools.model_1);
Tools.list_2.setModel(Tools.model_2);
System.out.println(Tools.ip + ":" + Tools.port + " is online!");
}else{
System.out.println(Tools.ip + ":" + Tools.port + " is not online!");
}
if(Tools.port != Tools.lastPort){
Tools.port++;
}
}
public static boolean serverIsOnline() {
try (Socket s = new Socket(Tools.ip, Tools.port)) {
return true;
} catch (IOException ex) {
}
return false;
}
}
答案 0 :(得分:0)
从你的问题不清楚实际问题是什么。
如果是线程报告服务器在线,但GUI显示脱机,则可能是因为您正在更新非事件线程上的Swing组件。您应该使用SwingUtilities.invokeLater更新组件。
<强>更新强>
正如boxed__l正确指出的那样,您正在静默地吞下ping方法中的异常。打印堆栈跟踪可以更好地了解发生的情况。
public static boolean serverIsOnline() {
try (Socket s = new Socket(Tools.ip, Tools.port)) {
return true;
} catch (IOException ex) {
// log the exception here, at the very least:
ex.printStackTrace();
}
return false;
}