Merlin's Magic: Java Networking enhancements

Source: Internet
Author: User
Tags resource versions

Networking in Java programming includes the ability to locate and identify resources and communicate over TCP and UDP connections. First, you need to identify a resource with a name like www.ibm.com, then open the connection to that resource, and finally send the packet between yourself and the other end of the connection. Other tasks may be included for security reasons, but the whole process is the same. For the Java platform, the classes that support these operations are found in the java.net package. From the early days of Java programming to the present, most of these operations have never changed much. However, with the development of Merlin, some of these basic operations have been improved to support valuable new features. In this article, we'll look at five of these features: IPV6 support, URIs, network interfaces, unbound sockets, and Secure sockets.

Support for IPV6 addresses

First, let's look at new support for the next Generation Internet Protocol V6 (Internet Protocol V6,ipv6) addressing architecture. With the help of InetAddress's two new subclasses (Inet4address and inet6address), you can connect to applications that are based on TCP and UDP. Inet4address supports older (and usually unique) IP addressing styles supported by most machines, and the localhost format is 127.0.0.1. The new addressing scheme defined in RFC2373 (see Resources) provides a colon-delimited format in which 0:0:0:0:0:0:0:1 is an equivalent loopback address to 127.0.0.1. The new class allows the application to support one or both of the addressing schemes.

Support for IPV6 depends on whether the underlying platform supports it, and the Solaris 8 and later versions, as well as the Linux 2.1.2 and higher (RedHat 6.1+) versions, support IPV6, which Microsoft Windows does not support (Microsoft's Win The Dow 2000 implementation is a limited implementation. I hope that the Windows version of J2SE 1.4 will support IPV6 later.

Understanding Uniform Resource Identifiers

The java.net package now includes a Uniform Resource Identifier (Uniform Resource Identifier,uri) class. A URI can be thought of as a Uniform Resource Locator (Uniform Resource Locator,url) that does not have a protocol handler behind the scenes. Typically, URLs look like http://www.ibm.com. In order for the Java language runtime to understand URLs, it needs to know how to handle information that starts with http:. Previously, if you proposed a new protocol (for example, like jdbc:database), you cannot treat a jdbc:database string as a URL without a protocol handler. Instead, you have to strictly treat it as a string, which is what JDBC is doing now.

The typical format for a URI is: [scheme:][//authority][path][?query][#fragment], where authority is usually the host name. However, it can also include user login information and ports: [userinfo@]host[:p ort]. The URI class itself provides a series of getter methods to understand the specific parts of the URI. You should use this class where you previously passed a string that looked like a URL (but only to describe the URL rather than use it).

List Network connections with NetworkInterface

Have you ever wondered which networking interface is available, but don't know how to ask without reverting to native code? Typically, most machines connected to the Internet have two connections: a local loop to itself and a connection to their local service provider. However, some machines are multihomed. They have multiple network adapters, each with a separate connection to the Internet and each with its own name and address. With this new NetworkInterface interface, you can specify which NIC to use when sending multicast data outward, or to see if the network connection is normal. Listing 1 illustrates the use of this class:

Listing 1. List network Interfaces

import java.net.*;
import java.util.Enumeration;
public class Nets {
  public static void main(String args[]) throws SocketException {
   Enumeration enum = NetworkInterface.getNetworkInterfaces();
   while (enum.hasMoreElements()) {
    NetworkInterface net = (NetworkInterface)enum.nextElement();
    System.out.println(
     "Names: " + net.getName() + " / " + net.getDisplayName());
    Enumeration enum2 = net.getInetAddresses();
    while (enum2.hasMoreElements()) {
     InetAddress address = (InetAddress)enum2.nextElement();
     System.out.println("\tAddress: " + address.getHostAddress());
    }
   }
  }
}

The results you run on this program are certainly different. Listing 2 includes the sample output you want to see:

Listing 2. The sample results in Listing 1

Names: lo / MS TCP Loopback interface
     Address: 127.0.0.1
Names: eth0 / 3Com EtherLink PCI
     Address: 192.168.0.109

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.