简体   繁体   English

如何从正在侦听广播的多播套接字获取IP地址?

[英]How can I get the IP address from a multicast socket that is listening to broadcasts?

My server creates a multicast socket which listens for UDP packets. 我的服务器创建了一个侦听UDP数据包的多播套接字。 It is receiving packets sent to the broadcast address of the network but I can't get the ip address of the sender: 它正在接收发送到网络广播地址的数据包,但我无法获得发送方的IP地址:

multisocket.getInetAddress().getHostAddress();

returns 回报

"::" 

(I guess its because of null getInetAddress). (我猜这是因为空的getInetAddress)。

How can I get the IP address of the sender? 如何获取发送者的IP地址?

TIPS: I guess it has to do with the socket not being bound and basically the whole broadcasting because those packets arent sent exclusively to me but to the whole network, but shouldnt they hold the IP address of the sender? 提示:我想这与套接字没有被绑定以及基本上整个广播有关,因为那些数据包不专门发送给我,而是发送给我,但发送给整个网络,但是它们不应该保留发送者的IP地址吗? Enlighten me please. 请赐教。

Here is the code: 这是代码:

public void run() {
    try {
        Thread.sleep(5000);
        Log.i("SERVERUDP", "Connecting...");
        MulticastSocket multisocket = new MulticastSocket(SERVERPORT);
        multisocket.setBroadcast(true);
        Log.i("SERVERUDP","Server's IP is: " + multisocket.getLocalAddress().getHostAddress());
        getLocalIpAddress();
        while(true){
            byte[] b = new byte[65535];
            ByteArrayInputStream b_in = new ByteArrayInputStream(b);
            DatagramPacket dgram = new DatagramPacket(b, b.length);
            multisocket.receive(dgram); // blocks
            ObjectInputStream o_in = new ObjectInputStream(b_in);
            Object o = o_in.readObject();
            dgram.setLength(b.length);
            b_in.reset();
            if(o.getClass().getSimpleName().equalsIgnoreCase("Request")){
                Request request = (Request)o;
                String inetaddress = multisocket.getInetAddress().getHostAddress();
                Log.i("SERVERUDP-if", "Sending request to IP: " + inetaddress);
                new Thread(new ClientTCP(inetaddress, createRequestFromBroadcast(request))).start();
            }else if(o.getClass().getSimpleName().equalsIgnoreCase("String")){
                Log.e("SERVERUDP-elseif-string", "WTF received a string: " + (String)o);
            }else{
                Log.e("SERVERUDP-else", "Unrecognized object of type: " + o.getClass().getSimpleName());
            }
            o_in.close();
            //iteration done only once for testing!
            break;
        }
        multisocket.close();
    } catch (Exception e) {
        Log.e("SERVERUDP", "Error", e);
    }
}

Each packet that you receive might have a different source address. 您收到的每个数据包可能具有不同的源地址。 So I'm not sure why you're trying to look at multisocket to figure out the source address. 所以我不确定为什么您要尝试使用multisocket来找出源地址。

I have to admit I haven't tried this, but does dgram.getSocketAddress () give you what you want after the multisocket.receive call returns? 我必须承认我还没有尝试过,但是dgram.getSocketAddress ()是否在multisocket.receive调用返回后为您提供所需的内容?

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

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