IP自动发现 [英] IP Autodiscovery

查看:405
本文介绍了IP自动发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要网络设备:  1.服务器(可变IP)需要接收的字符串进行进一步的东西(例如Socket 9999)。该服务器还具有另外一个插座(如8888),在那里将它在配对的设备名称。  2.客户端(可变IP),做的不可以知道服务器的IP,但要送他的字符串。

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 sends it's device name on pairing. 2. Client (variable IP) that does NOT know the IP of the server but wants to send him the string.

在一个IP C-网络,我可以遍历的最后一个字节(0..255),并检查插座8888传送的东西。但在A和B网我就没有机会了。有没有其他的解决方案呢? (我可以遍历所有四个八位字节,但是这不会是一个完美的解决方案)。

On a IP C-network I could iterate through the last octet (0..255) and check if Socket 8888 transmits something. But on A and B networks I have no chance. Is there any other solution for this? (I could iterate through all four octets but that wouldn't be an elegant solution).

感谢您!

推荐答案

最适当的方法来做到这一点,如果他们在同一个局域网是:

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

  • 在客户端发送一个UDP广播到一个特定的端口和匹配网络类(A,B,C)
  • 服务器侦听此端口上,接收广播数据包,连接或传送,他的IP给客户端。

使用只有两个网络数据包,你知道的IP地址。

With just two network packets you know the IP address.

- EDITED -

--EDITED--

要播出:

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<NetworkInterface> 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<InterfaceAddress> intfaddrs = intf.getInterfaceAddresses();
                return intfaddrs.get(0).getBroadcast(); //return first IP address
            }
        }
    }
    return null;
}

要receice广播:

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.

这篇关于IP自动发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆