简体   繁体   English

使用eclipse(windows)在java中使用IPv6地址和端口号创建套接字

[英]Socket creation using a IPv6 Address and port number in java using eclipse(windows)

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 : 当我尝试使用IPv4地址创建套接字时,它很成功,但是当我尝试使用IPv6地址和端口号创建套接字时,它会引发异常:

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. 我通过Eclipse IDE从Windows机器运行代码。

    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);
    }

Typically this error means the V6 Address for the remote machine is not actually binding to the given port. 通常,此错误表示远程计算机的V6地址实际上并未绑定到给定端口。 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. 例如,如果您可以通过端口80上的IPV4与Web服务器通信并不意味着它实际上将本地V6地址绑定到端口。 You can still ping the machine but ping doesn't actually tell you if the Port is bound. 您仍然可以ping机器,但ping实际上并不会告诉您端口是否绑定。

A quick test to prove the way your using socket is correct is: 一个快速测试来证明你的使用套接字是正确的方式是:

On the Eclipse Windows Machine 在Eclipse Windows机器上

  1. At the command prompt do a netstat -p TCPv6 -a 在命令提示符下执行netstat -p TCPv6 -a
  2. Pick one of the localhost "::" entries. 选择一个localhost“::”条目。 In this example my machine is returning [::]:135 as one of the current V6 bound ports. 在这个例子中,我的机器返回[::]:135作为当前V6绑定端口之一。

Try connecting in Eclipse: 尝试在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. 我知道上面的内容对你当前的代码没有帮助,但更多的目的是为了隔离IPV6地址和端口#没有绑定并且Socket连接异常有效的可能性。

Another test I did is I grabbed the Inet6 address for my Linux machine and insured SSH was listening on that port. 我做的另一个测试是我抓住了我的Linux机器的Inet6地址,并确保SSH正在侦听该端口。 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. 然后我在我的Windows eclipse上使用了相同的代码,并针对我的Linux Inet6地址和端口22执行了套接字。这也很好。 My local tomcat failed because it wasn't setup to listen on Inet6. 我的本地tomcat失败了,因为它没有设置为在Inet6上侦听。

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

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