繁体   English   中英

即使服务器处于联机状态,对服务器进行ping操作也说它处于脱机状态?

[英]Pinging a server says offline even if it's online?

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

 }

从您的问题尚不清楚实际的问题是什么。

如果是线程报告服务器已联机,但GUI显示为脱机,则可能是因为您正在更新非事件线程上的Swing组件。 您应该使用SwingUtilities.invokeLater更新组件。

更新

box__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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM