2

In my app, I create a ServerSocket, and wait for connections:

while(isRunning) {
    try {
        socket = serverSocket.accept();

I then try to get the remote IP of the resulting socket:

socket.getInetAddress().getHostAddress();

However, this seems to only return an IPv6 address.

For my purposes, I believe I need an IPv4. Is there some way to get an IPv4 address from a socket?

yydl
  • 24,284
  • 16
  • 65
  • 104

3 Answers3

1

don't know if it's too late or not, but this little piece of code could solve your problem. I had same problem today and found this:

private String getLocalIpAddress() {
   try {
       for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
           NetworkInterface intf = en.nextElement();
           for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
               InetAddress inetAddress = enumIpAddr.nextElement();
               if (!inetAddress.isLoopbackAddress()) {
                   if (inetAddress instanceof Inet4Address) {
                       return ((Inet4Address)inetAddress).getHostAddress().toString();
                   }
               }
           }
       }
   } catch (SocketException ex) {
       Log.e("ServerActivity", ex.toString());
   }
   return null;
}

The main point is to test if IP address is really a IP V4 address.

Have fun

Louis-Marie
  • 499
  • 4
  • 7
  • 1
    I don't think you understand the question. I don't need the local ip. I need the ip of the client that connected to my serversocket – yydl Jul 29 '12 at 20:45
0

If the client has connected to an IPv6 address (i.e., if your server is IPv6 hosted), then you'll only have the IPv6 address. You'll need to host you server in IPv4 space as well (or instead of), then have the client connect to the IPv4 address in order to get the IPv4 address of the client.

You may be able to do a lookup via DNS, but this won't be reliable (i.e., do reverse lookup of IPv6 address to get hostname, then do a forward lookup, looking for A records on the host). This assumes that reverse DNS has been set up though for the address, which isn't guaranteed.

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • "You'll need to host you server in IPv4 space as well (or instead of)," - do you know how I would do this? – yydl Dec 11 '11 at 22:58
  • It's a case of listening on the IPv4 address of the server. If the server only has an IPv6 address, then this isn't possible. Otherwise it's a case of listening on all ports. How are you creating your ServerSocket? By default, it listens on all IP addresses unless you've explictly provided an IP address when creating the object. – Chris J Dec 12 '11 at 10:10
  • I'm using `ServerSocket serverSocket = new ServerSocket(port)` which I believe does listen on all IPs. I also know that the server does in fact have both an IPv6 and IPv4. Nonetheless, I think the client is always choosing to connect to the IPv6. – yydl Dec 12 '11 at 17:03
  • You need to force the client to connect to the IPv4 address; if you haven't got any control over the client source code, then this will mean fiddling around with the system configuration somehow as even Windows prefers IPv6 over IPv4. – Chris J Dec 12 '11 at 19:54
-1

While some IPv6 addresses map to the IPv4 range it is not possible to convert all IPv6 to IPv4, as there are more IPv6 addresses than there are IPv4 addresses. Oracle has a really good document explaining IPv6 and IPv4 and how it impacts on Java and networking. You should also check out the InetAddress API document. I've linked both below:

http://docs.oracle.com/javase/1.4.2/docs/guide/net/ipv6_guide/

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html

Deco
  • 3,261
  • 17
  • 25
  • In my case, my router claims that the "client" (i.e. one connecting to my serversocket) happens to have an IPv4. Where does that fit in? – yydl Dec 11 '11 at 23:02
  • Why is it you have to have an IPv4? I see from your comment on the question you dont believe `MediaPlayer` (?) supports IPv6. Does this means that if a "client" has an IPv6 address, your application inherently wont support it? – Deco Dec 12 '11 at 00:56
  • assuming that is so, it is beyond my control. MediaPlayer seems leaves me no choice. I'm assuming that most routers assign two IPs to everyone, so since my clients are normally on the local network, they should have one available. At least in my test case that appears to be the case. – yydl Dec 12 '11 at 02:59