4

When I try to create a socket using IPv4 address, it's successful, but when I try to create a socket using IPv6 address and port number it throws an exception :

java.net.SocketException: Network is unreachable: connect
          at java.net.DualStackPlainSocketImpl.connect0(Native Method)
          at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
          at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
          at java.net.PlainSocketImpl.connect(Unknown Source)
          at java.net.SocksSocketImpl.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.connect(Unknown Source)
          at java.net.Socket.<init>(Unknown Source)
          at java.net.Socket.<init>(Unknown Source)
          at epcs.intf.be.SimConnectionHandler.connect(SimConnectionHandler.java:332)
          at epcs.intf.be.BackEndConnection.connect(BackEndConnection.java:42)
          at epcs.intf.be.ProcedureRunner.runScenario(ProcedureRunner.java:230)
          at epcs.exec.Runner.SendExecTrigger(Runner.java:418)
          at epcs.exec.Runner.sendCommand(Runner.java:454)
          at epcs.main.ExecutionThread$TaskStartTestCaseExecution.run(ExecutionThread.java:98)
          at epcs.main.ExecutionThread.run(ExecutionThread.java:29)

Code:

I am running the code through Eclipse IDE from a Windows machine.

    if(p_objSimData.getIpAddress().contains(":") )
    {
        System.out.println("IPV6 Address Found\n");
        InetAddress ip6addr = Inet6Address.getByName(p_objSimData.getIpAddress());
        System.out.println("InetAddress ip6addr = "+ip6addr); **//prints  //2011::11 - IPv6 address**
        objConnection.m_objSocket = new Socket(ip6addr,p_objSimData.getPortNo()); -  **here it is throwing an exception** 
    }
    else
    {
        objConnection.m_objSocket = new Socket(p_objSimData.getIpAddress(),p_objSimData.getPortNo());
        //m_hmObjConnection1.put(p_objSimData.getIpAddress(), objConnection.m_objSocket);
        m_hmObjConnection1.put(p_objSimData.getIpPort(), objConnection.m_objSocket);
    }
Raul Rene
  • 10,014
  • 9
  • 53
  • 75
user1696887
  • 41
  • 1
  • 3
  • Is that IP address reachable? Is the server application properly bound to it? – E_net4 Sep 25 '12 at 11:31
  • 1
    Does the Windows machine have IPv6 installed and enabled? – Remy Lebeau Sep 25 '12 at 18:20
  • I am not sure if that is related, but are you using java 7? I know that there is a bug in the JDK that can cause socket problems with IPv6 on windows machines. – Christian Beikov Sep 26 '12 at 03:18
  • I can run the same code even on Linux but there the exception being thrown is java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source at java.net.Socket.(Unknown Source) at epcs.intf.be.SimConnectionHandler.conect(SimConnectionHandler.java:332 – user1696887 Sep 26 '12 at 04:43
  • Im am able ping the system using the same Ipv6 address, but during Socket Creation it is throwing an exception. – user1696887 Sep 26 '12 at 06:53
  • You may find this of help: http://stackoverflow.com/questions/10378471/how-to-support-both-ipv4-ipv6-on-java – Menelaos Apr 08 '13 at 22:10

1 Answers1

2

Typically this error means the V6 Address for the remote machine is not actually binding to the given port. For example if you can talk to a web server via IPV4 on port 80 doesn't mean it's actually binding the local V6 address to the port. You can still ping the machine but ping doesn't actually tell you if the Port is bound.

A quick test to prove the way your using socket is correct is:

On the Eclipse Windows Machine

  1. At the command prompt do a netstat -p TCPv6 -a
  2. Pick one of the localhost "::" entries. In this example my machine is returning [::]:135 as one of the current V6 bound ports.

Try connecting in Eclipse:

public static void main(String[] args) {
  try
  {
    InetAddress address = InetAddress.getByName("::");
    Socket socket = new Socket(address, 135);
    // Should of connected with no exception thrown since we know this port was listening in netstat

  }
  catch (Throwable t) {
    t.printStackTrace();
  }
}

I know the above doesn't help you with your current code but it's more aimed to isolate the possibility that the IPV6 Address and Port # are not bound and the Socket connection exception is valid.

Another test I did is I grabbed the Inet6 address for my Linux machine and insured SSH was listening on that port. I then used the same code above from my Windows eclipse and did the socket against my Linux Inet6 address and port 22. This worked fine as well. My local tomcat failed because it wasn't setup to listen on Inet6.

Jeremy Unruh
  • 649
  • 5
  • 10