Android Lollipop (5.1.1) - How to use IPV6 address when creating a socket connection
<p>I'm putting together some proof of concept code to send a string over a wifi connection to a linux server.  I managed this when using an IPV4 address but now I am having difficulty adding support for IPV6.  The error reports that my IPV6 address isn't valid.</p>

<p>I did find this SO thread... <a href="https://stackoverflow.com/questions/13421962/ipv6-socket-on-android">IPv6 Socket on Android</a> but that is from years ago.  Perhaps things are done differently now?  (I've noticed that 3 years is a long, long time in Android).</p>

<p>Here's the function I'm trying to get working (I've taken out a bunch of unrelated code to make it look a bit less cluttered)...</p>

<pre><code>private void SocketTest()
{
    Socket socket = null;
    DataOutputStream dataOutputStream = null;

    try
    {
        Inet6Address ipv6_addr = null;
        NetworkInterface nif = null;

        nif = NetworkInterface.getByName("wlan0");

        if(nif != null)
        {
            //HOSTNAME is "fred"
            //HOST_IPV6_ADDR is "fe80::7e5c:f8ff:fe3b:e7c3"
            //ERROR occurs on next line...
            ipv6_addr = Inet6Address.getByAddress(HOSTNAME, HOST_IPV6_ADDR.getBytes(), nif);
        }

        if(ipv6_addr != null)
            socket = new Socket(ipv6_addr, 12345);

        //socket = new Socket("192.168.88.184", 12345);
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataOutputStream.writeUTF("in SocketTest()\n");
        socket.close();
    } catch (UnknownHostException e) {
//[... unrelated stuff ...]
}
</code></pre>

<p>I get the following error during the call to Inet6Address.getByAddress(...):</p>

<pre><code>java.net.UnknownHostException: Not an IPv6 address: [102, 101, 56, 48 ... 99, 51]
</code></pre>

<p>Further info: this is the remote linux server's wlan0 info (it only has 1 wifi interface)...</p>

<pre><code>wlan0     Link encap:Ethernet  HWaddr 7c:5c:f8:3b:e7:c3
          inet6 addr: fe80::7e5c:f8ff:fe3b:e7c3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2453 errors:0 dropped:1 overruns:0 frame:0
          TX packets:2445 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:108860 (106.3 KiB)  TX bytes:152518 (148.9 KiB)
</code></pre>

<p>The IPV4 version of this code worked as you would expect, the remote server is operational.  The remote server also has an IPV6 address on it's wired ethernet port and I can ssh into that IPV6 address just fine from another machine on the same wired LAN.  Ergo my code is broken.</p>

<p>Can someone explain what my code is doing wrong or missing out?</p>

<p>Sorry if this seems like a duplicate question but there doesn't seem to be much recent information on this topic as far as I can see.</p>

<hr>

<p><strong>EDIT</strong></p>

<p>I should have used this instead of a string to hold the IPv6 address...</p>

<pre><code>//"fe80::7e5c:f8ff:fe3b:e7c3";
    private final byte[] HOST_IPV6_ADDR = {(byte)0xfe
                                         , (byte)0x80
                                         , 0x00
                                         , 0x00
                                         , 0x00
                                         , 0x00
                                         , 0x00
                                         , 0x00
                                         , 0x7e
                                         , 0x5c
                                         , (byte)0xf8
                                         , (byte)0xff
                                         , (byte)0xfe
                                         , 0x3b
                                         , (byte)0xe7
                                         , (byte)0xc3
    };
</code></pre>

<p>Thank you <code>StenSoft</code></p>