Practical session on Java IPv6 Programming

InetAddress.getByName


import java.io.*; 
import java.net.*;

InetAddress ia=InetAddress.getByName("www.garr.it");

//or  InetAddress ia=InetAddress.getByName(“[::1]");  
//or "::1" 

String host_name = ia.getHostName();
System.out.println( host_name ); // ip6-localhost

String addr=ia.getHostAddress();
System.out.println(addr); //print IP ADDRESS

InetAddress.getAllByName

import java.io.*; 
import java.net.*;

InetAddress[ ] alladr=ia.getAllByName("www.kame.net");
for(int i=0;i<alladr.length;i++) { System.out.println(  alladr[i]  );  }

expected output (1)

print:
  www.kame.net/203.178.141.194
  www.kame.net/2001:200:0:8002:203:47ff:fea5:3085

NetworkInterface class usage example

import java.io.*; 
import java.net.*;

Enumeration netInter = NetworkInterface.getNetworkInterfaces();
 while ( netInter.hasMoreElements() )
 {
   NetworkInterface ni = (NetworkInterface)netInter.nextElement();
   System.out.println( "Net. Int. : "+ ni.getDisplayName() );
   Enumeration addrs = ni.getInetAddresses();
     while ( addrs.hasMoreElements() )
     {
       Object o = addrs.nextElement();
       if ( o.getClass() == InetAddress.class ||
            o.getClass() == Inet4Address.class || 
            o.getClass() == Inet6Address.class )
       {
         InetAddress iaddr = (InetAddress) o;
         System.out.println( iaddr.getCanonicalHostName() );               
         System.out.print("addr type: ");
         if(o.getClass() == Inet4Address.class) {&#8230;println("IPv4");}
         if(o.getClass() == Inet6Address.class){&#8230;println( "IPv6");}
         System.out.println( "IP: " + iaddr.getHostAddress() );
         System.out.println("Loopback? "+iaddr.isLoopbackAddress());
         System.out.println("SiteLocal?"+iaddr.isSiteLocalAddress());
         System.out.println("LinkLocal?"+iaddr.isLinkLocalAddress());
       }
     }
 }

expected output (2)

Net. Int. : eth0
-------------------------
CanonicalHostName: fe80:0:0:0:212:79ff:fe67:683d%2
addr type: IPv6   IP: fe80:0:0:0:212:79ff:fe67:683d%2
Loopback? False  SiteLocal? False   LinkLocal? true
-------------------------
CanonicalHostName: 2001:760:40ec:0:212:79ff:fe67:683d%2
addr type: IPv6   IP: 2001:760:40ec:0:212:79ff:fe67:683d%2
Loopback? False   SiteLocal? False   LinkLocal? false
-------------------------
CanonicalHostName: pcgarr20.dir.garr.it
addr type: IPv4   IP: 193.206.158.140
Loopback? False   SiteLocal? False  LinkLocal? false

Net. Int. : lo
-------------------------
CanonicalHostName: ip6-localhost
addr type: IPv6   IP: 0:0:0:0:0:0:0:1%1
Loopback? True   SiteLocal? False   LinkLocal? false
-------------------------
CanonicalHostName: localhost
addr type: IPv4   IP: 127.0.0.1
Loopback? True   SiteLocal? False   LinkLocal? false


A basic TCP/IP Client/Server

Server Code

import java.io.*; 
import java.net.*;

ServerSocket serverSock = null;  
Socket cs = null;   

try {
 serverSock = new ServerSocket(5000); 
 cs = serverSock.accept(); 
 BufferedOutputStream b = new
             BufferedOutputStream(cs.getOutputStream());
 PrintStream os = new PrintStream(b,false);
 os.println(&#8220;hallo!&#8221;); os.println("Stop"); 

 cs.close();
 os.close();
}catch (Exception e) { 
  System.out.println("You cought an exception, man!"
}

Client Code

import java.io.*; 
import java.net.*;

Socket s = null; DataInputStream is = null;

try {
  s = new Socket("localhost", 5000);
  is = new DataInputStream(s.getInputStream());
  String line;
  while( (line=is.readLine())!=null ) {
    System.out.println("received: " + line);
    if (line.equals("Stop")) break;
  }
  is.close(); 
  s.close();
}catch (IOException e) {
        System.out.println("You cought an exception, man!"
     }

-- MarioReale - 20 Dec 2007

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r3 - 2008-01-15 - MarioReale
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    EGEE All webs login

This site is powered by the TWiki collaboration platform Powered by Perl This site is powered by the TWiki collaboration platformCopyright & by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Ask a support question or Send feedback