JDK/JRE5.0中对于IPv6的支持-解读JDK5.0对IPv6网…

2008-02-23 10:05:32来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


JDK5.0 Document:Networking IPv6 User Guide for JDK/JRE 5.0


This document covers the following topics:

  • Overview
  • Supported Operating Systems
  • Using IPv6 in Java
  • Details on IPv6 Support in Java
    • Special IPv6 Address Types
    • IPv6-Related System Properties
    • Dual-Stack Node
    • Java Application Impact
    • IPv6 Networking Properties

Overview

Within the past few years IPv6 has gained much greater acceptance in the industry, especially in certain regions of the world, i.e., Europe and the Asian Pacific. Extensibility, mobility, quality of service, larger address space, auto-configuration, security, multi- homing, anycast and multicast, and renumbering—these are some of the features of IPv6 that make it desirable.

With the release of J2SE 1.4 in February 2002, Java began supporting IPv6 on Solaris and Linux. Support for IPv6 on Windows was added with J2SE 1.5. While other languages, such as C and C can support IPv6, there are some major advantages to Java:

  • With Java you invest in a single code base that is both IPv4- and IPv6-ready.
  • Your existing Java applications are already IPv6-enabled.
  • Migration to IPv6 is easy

We will prove these statements with code examples below and provide additional details on IPv6 support.

个人观点:通过一些移植的工作,本人认为Java在IPv6支持和移植方面确实有自身的优势,由于是面向对象的语言,地址类InetAddress很容易的派生出Inet6Address和Inet4Address,在代码的改动上可以说非常的小,尤其针对于C语言。就像JDK帮助文档里提到的,Java网络代码向IPv6过渡是非常简单的,代码本身可以轻松的过渡到v4/v6双支持,这对Java程序员来说确实是一大幸事。

Supported Operating Systems

The following operating systems are now supported by the J2SE reference implementation:

  • Solaris 8 and higher
  • Linux kernel 2.1.2 and higher (kernel 2.4.0 and higher recommended for better IPv6 support)
  • Windows XP SP1 and Windows 2003

个人观点:JDK1.4.2对于Solaris and Linux中IPv6的支持没有什么问题,但在Windows环境下Java对IPv6的支持确实有限,可能这和当时windows版本本身对IPv6支持的局限性有关。记得曾经在WindowXP下面用JDK1.4.2试图编写过IPv6的网络程序,但是ServerSocket绑定的时候就会报错-协议类型不支持。

目前,Windows2000/XP/2003对于IPv6都提供了基本的支持,JDK1.5在Windows环境中对于IPv6的支持是理所应当的了。

Using IPv6 in Java

Using IPv6 in Java is easy; it is transparent and automatic. Unlike in many other languages, no porting is necessary. In fact, there is no need to even recompile the source files.~确实是令人兴奋,很多工作在JDK中做好了,上层的改动就简单多了!

Consider an example from The Java Tutorial:

Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
"the connection to: taranis.");
System.exit(1);
}
// ... code omitted here
communicateWithEchoServer(out, in);

out.close();
in.close();
stdIn.close();
echoSocket.close();

You can run the same bytecode for this example in IPv6 mode if both your local host machine and the destination machine (taranis) are IPv6-enabled.

从客户端的角度来讲,如果地址解析是通过主机名或域名的方式,那客户端的代码几乎可以不做改动

In contrast, if you wanted the corresponding C program to run in IPv6 mode, you would first need to port it. Here's what would need to happen:

以下的情况在v4到v6的移植过程中是非常常见的,工作量确实不小,而且还要看代码中对于地址的封装程度,对于那种满篇代码到处绑定地址结构还不清楚的代码,移植过程是非常痛苦的!

ExcERPt of original C code:

struct sockaddr_in sin;
struct hostent *hp;
int sock;

/* Open socket. */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
return (-1);
}

/* Get host address */
hp = gethostbyname(hostname);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:J2EE学习之-搭建开发平台

下一篇:[Eclipse笔记]关于3.1M5a的性能