Java, DatagramPacket receive, how to determine local ip interface
Asked Answered
P

2

8

Here's simple case when binding to all ip interfaces and specific udp port:

int bindPort = 5555;  // example, udp port number
DatagramSocket socket = new DatagramSocket(bindPort);

byte[] receiveData = new byte[1500];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

...

socket.receive(receivePacket);

How do I know on which ip interface I received packet ?

I can see there is getSocketAddress():

Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.

but that returns remote ip+port. I would like to know local ip (local port is 5555 in this example).

Is it possible with std. Java libraries ?

Pursuance answered 26/2, 2014 at 14:59 Comment(4)
I don't understand, you already know the port since you're passing it. Local IP address is 127.0.0.1.Avian
Isn't the local IP address ALWAYS 127.0.0.1?Cassaundracassava
you are right about port, it's obvious. But local address can be any, since I bound to 0.0.0.0. Get it ?Pursuance
InetAddress IP=InetAddress.getLocalHost(); System.out.println("IP of my system is := "+IP.getHostAddress()); Stolen from https://mcmap.net/q/11202/-getting-the-ip-address-of-the-current-machine-using-javaCassaundracassava
P
5

I know this problem. For clarifying the purpose of this: You receive a packet over UDP (normally some kind of discovery implementation) and want return an anwer like ("Dear client, please download the file from http://<wtf-is-my-ip>:8080/thefile.txt"). My machine has three IPs: 127.0.0.1, 192.168.x.x and 10.x.x.x and the goal is to find out that the remote client sent the UDP packet to 192.168.x.x and that this must be the IP which will also work for another connection.

From the weupnp project I found the following code, which is not perfect but worked for me:

    private InetAddress getOutboundAddress(SocketAddress remoteAddress) throws SocketException {
        DatagramSocket sock = new DatagramSocket();
        // connect is needed to bind the socket and retrieve the local address
        // later (it would return 0.0.0.0 otherwise)
        sock.connect(remoteAddress);

        final InetAddress localAddress = sock.getLocalAddress();

        sock.disconnect();
        sock.close();
        sock = null;

        return localAddress;
    }

//DatagramPacket receivePacket;
socket.receive(receivePacket);
System.out.print("Local IP of this packet was: " + getOutboundAddress(receivePacket.getSocketAddress()).getHostAddress);

The code might return the wrong IP if you have multiple IPs in the same network or because of some advanced routing configuration. But so far it's the best I was able to find and it's enough for most situations.

Pyramidal answered 12/8, 2015 at 13:7 Comment(0)
C
0

You can use a combination of NetworkInterface and InetAddress classes in order to get the details of your network.

NetworkInterface

InetAddress

Cup answered 26/2, 2014 at 15:10 Comment(1)
if I use tcp, and ServerSocket class, there is getLocalSocketAddress() which returns exactly what I need. But with udp, I couldn't find anything like that.Pursuance

© 2022 - 2024 — McMap. All rights reserved.