有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ping服务器时,即使服务器处于在线状态,也表示服务器处于离线状态?

我试图查看一些服务器是否在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;
}

 }

共 (1) 个答案

  1. # 1 楼答案

    从你的问题中不清楚实际的问题是什么

    如果线程报告服务器处于联机状态,但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;
    }