我需要知道如何使用Java检测以太网电缆是否已插入或拔出,​​我使用NetworkInterface来检查接口,但是当电缆拔出并再次插入时,我需要发出警报。

ArrayList<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

    for (NetworkInterface info : interfaces){
        if(info.isUp()){
            System.out.println(info.getName());
        }
    }

最佳答案

这是旧线程,但可能对研究人员有用。
根据J R的响应进行了修改。

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class EthernetCableDetector implements Runnable{

    private NetworkInterface iNet;
    private boolean isRunning = true;
    private BooleanProperty pluged = new SimpleBooleanProperty(false);

    public boolean isPluged() {
        return pluged.get();
    }

    public BooleanProperty plugedProperty() {
        return pluged;
    }

    public void setPluged(boolean pluged) {
        this.pluged.set(pluged);
    }

    public EthernetCableDetector(String localAddress) {
        /**
         * Acquire the list of interfaces available in your OS.
         */
        Enumeration<NetworkInterface> tempNetInterface = null;
        try {
            tempNetInterface = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException ex) {
            ex.printStackTrace();
        }
        ArrayList<NetworkInterface> interfaces = new ArrayList<>(Collections.list(tempNetInterface));

        /**
         *Make another list containing only the real interfaces.
         * Remember that depending on the machine and OS the condition if (iNet.getHardwareAddress() != null)
         * may not be enought to find out if this interface is real or not, the method isVirtual() won't give
         * you the truth but there are many other solutions for this problem.
         */
        ArrayList<NetworkInterface> realInterfaces = new ArrayList<>(Collections.list(tempNetInterface));
        for (NetworkInterface iNet : interfaces) {
            try {
                if (iNet.getHardwareAddress() != null) { //This verification might not be enought to find if a Interface is real.
                    realInterfaces.add(iNet);
                }
                /** From the real interface you should identify which ones are ethernet connections.
                 *  (if they are wireless there will be no cables involved). For windows you can verify
                 *  if the method getName() returns something like "eth0" or "eth1", if yes it is a ethernet connection.
                 */
                if (iNet.getName().contains("eth") && iNet.getInetAddresses().hasMoreElements() && iNet.getInetAddresses().nextElement().getHostAddress().equals(localAddress)) {
                    System.out.println(localAddress + " Found.");
                    this.iNet = iNet;
                    /*
                    boolean previousStatus = iNet.isUp();
                    if (iNet.isUp() != previousStatus) {
                        // Interface cable must habe been plugged or unplugged.
                        previousStatus = iNet.isUp();
                        System.out.println("Cable status has changed " + iNet.isUp());
                    }
                    */
                }
            } catch (SocketException ex) {
                //TODO handle exception
            }
        }

    }

    @Override
    public void run() {
        Globals.pause(5000);
        boolean previousStatus = false;
        try {
            previousStatus = iNet.isUp();
            setPluged(previousStatus);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        System.out.println("Cable status = " + previousStatus);
        while(isRunning)
        {
            Globals.pause(250);
            try {
                setPluged(iNet.isUp());
                if (iNet.isUp() != previousStatus) {
                    // Interface cable must habe been plugged or unplugged.
                    previousStatus = iNet.isUp();
                    System.out.println("Cable status =" + iNet.isUp());
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop(){
        isRunning = false;
    }

    public static void main(String[] args) {
        new EthernetCableDetector("192.168.10.26").run();
    }

}

07-24 05:04