IP Autodiscovery

后端 未结 1 991
执念已碎
执念已碎 2021-02-11 07:32

I have to network devices: 1. Server (variable IP) that needs to receive a String for further stuff (e.g. Socket 9999). This server has also another socket (e.g. 8888) where it

1条回答
  •  一向
    一向 (楼主)
    2021-02-11 07:53

    The most appropriate way to do it, if they are in the same LAN is:

    • Client sends a UDP broadcast to a specific port and matching the network class (A,B,C)
    • Server is listening on this port, receives the broadcast packet and connect or send his IP to the client.

    With just two network packets you know the IP address.

    --EDITED--

    To broadcast:

    InetAddress broadcastAddr = SharedFunctions.getNetworkLocalBroadcastAddressdAsInetAddress();
    
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
            socket.setBroadcast(true);
            System.arraycopy(BROADCAST_SIGNATURE, 0, buffSend, 0, BROADCAST_SIGNATURE.length);
            DatagramPacket packet = new DatagramPacket(buffSend, buffSend.length, broadcastAddr, BROADCAST_PORT);
            socket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
            if(socket != null) try {socket.close();} catch (Exception e1) {}
        }
    
    
    public static InetAddress getNetworkLocalBroadcastAddressdAsInetAddress() throws IOException {
        for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            if(VERSION.SDK_INT < 9) { 
                if(!intf.getInetAddresses().nextElement().isLoopbackAddress()){
                    byte[] quads = intf.getInetAddresses().nextElement().getAddress();
                    quads[0] = (byte)255;
                    return InetAddress.getByAddress(quads);
                }
            }else{
                if(!intf.isLoopback()){
                    List intfaddrs = intf.getInterfaceAddresses();
                    return intfaddrs.get(0).getBroadcast(); //return first IP address
                }
            }
        }
        return null;
    }
    

    To receice broadcast:

            try {
                socketReceiver = new DatagramSocket(BROADCAST_PORT);
                socketReceiver.setBroadcast(true);
                DatagramPacket packet = new DatagramPacket(buffRecv, buffRecv.length);
                while(Thread.currentThread() == cThreadReceiver){
                    socketReceiver.receive(packet);
                    //here you receive the packet and can check the sender IP address
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(socketReceiver != null) try {socketReceiver.close();} catch (Exception e1) {}
            }
    

    You will need to do some editing but should start you in the right track.

    0 讨论(0)
提交回复
热议问题