顯示具有 Java網路程式 標籤的文章。 顯示所有文章
顯示具有 Java網路程式 標籤的文章。 顯示所有文章

2012年12月11日 星期二

取得Socket資訊

在命令列輸入一串主機名稱,並嘗試對各個主機開啟一個Socket
印出遠端主機、遠端通訊埠、本地位址及本地通訊埠

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

public class SocketInfo{
    public static void main(String[] args){
        for(int i = 0; i < args.length; i++){
            try{
                Socket s = new Socket(args[i], 80);
                System.out.println("Connected to " + s.getInetAddress()
                    + "on port " + s.getPort() + " from port "
                    + s.getLocalPort() + " of "
                    + s.getLocalAddress());
            }
            catch(UnknownHostException ex){
                System.err.println("I can't find " + args[i]);
            }
            catch(SocketException ex){
                System.err.println("Could not connect to " + args[i]);
            }
            catch(IOException ex){
                System.err.println(ex);
            }
        }//for end
    }//main end
}//SocketInfo end


參考資料:Java網路程式設計 第三版(歐萊禮出版) 第九章範例

下載網頁

在命令列輸入指定的URL,印出URL的原始資料
如果URL指向一個HTML檔案,則印出HTML的原始內容

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

public class DownloadHTML{
    public static void main(String[] args){
        if( args.length > 0 ){
            try{
                URL url = new URL(args[0]);
                InputStream in = url.openStream();
                in = new BufferedInputStream(in);
   
                Reader r = new InputStreamReader(in);
                int c;
                while((c = r.read()) != -1){
                    System.out.print((char) c);
                }
            }
            catch(MalformedURLException ex){
                System.out.println(args[0] + " is not a parseable URL");
            }
            catch(IOException ex){
                System.out.println(ex);
            }
        }//if end
    }//main end
}//DownloadHTML end


參考資料:Java網路程式設計 第三版(歐萊禮出版) 第七章範例

簡易nslookup

在命令列輸入主機名稱,印出IP位址
輸入IP位址,印出主機名稱

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

public class NsLookUp{
    public static void main(String[] args){
        if(args.length > 0){
            for(int i = 0; i < args.length; i++){
                System.out.println(lookup(args[i]));
            }
        }
    }
 
    public static String lookup(String host){
        InetAddress address;
  
        try{
            address = InetAddress.getByName(host);
        }
        catch(UnknownHostException ex){
            return "Cannot find" + host;
        }
  
        if(isHostName(host)){//host name
            return address.getHostAddress();
        }
        else{ //IP address
            return address.getHostName();
        }
    }
 
    public static boolean isHostName(String host){
        //IPv6
        if(host.indexOf(':') != -1) return false;
  
        char[] ch = host.toCharArray();
        for(int i = 0; i < ch.length; i++){
            if(!Character.isDigit(ch[i])){
                //host name
                if(ch[i] != '.') return true;
            }
        }
  
        //不是host name就是IP
        return false;
    }
}


參考資料:Java網路程式設計 第三版(歐萊禮出版) 第六章範例

2011年5月3日 星期二

查詢本機IP


import java.net.*;

public class LookIP {
   public static void main(String[] args){
      try{
         InetAddress host = InetAddress.getLocalHost();
         System.out.println("網域名稱: " + host.getHostName());
         System.out.println("IP位址: " + host.getHostAddress());
      }catch(Exception e){
         e.printStackTrace();
      }
   }
}