4/15/12

Viết chương trình xử lí chuỗi nhập, dùng nó để khởi tạo đối tượng URL và trả về thuộc tính của nó

Đề bài: Viết chương trình xử lí chuỗi nhập, dùng nó để khởi tạo đối tượng URL và trả về thuộc tính của nó
Chức năng chương trình:
-         Truy cập vào URL, sau đó in các thông tin của tài nguyên như:
o   Tên file
o   Tên host
o   Số hiệu cổng
o   Kiểu giao thức (https hoặc http hoặc ftp…)
Cơ chế:
-         Client gửi yêu cầu đến SERVER
-         SERVER trả về thông tin tài nguyên
-         Client xuất ra console


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.*;

public class ServerBai1 {
  
    private String url = "http://google.com";
    private ServerSocket server;
    DataOutputStream dos;
    DataInputStream din;

    public ServerBai1(int so) throws Exception{
        server=new ServerSocket(so);
        System.out.println("Server started!!!");
    }

        public String init() {
        String strcity="";
            try {
                //Truy cập vào URL
                URL u = new URL(url);
                //Sau đó in các thông tin của tài nguyên như
                System.out.println("Name of the file is:");
                System.out.println("Host Name is: "+u.getHost());
                System.out.println("Port number is: "+u.getPort());
                System.out.println("Protocol type is: "+u.getProtocol());
            }
            catch (MalformedURLException e)
                { System.out.println(e); }
            return strcity;
        }

        public void run(){
            try{
                boolean ck=true;
                Socket sk= server.accept();
                dos= new DataOutputStream(sk.getOutputStream());
                din = new DataInputStream(sk.getInputStream());
                while(true)
                {
                    if(ck){
                        dos.writeUTF(init());
                        ck=false;
                    }
                }
            }
            catch(Exception e){
            }
        }
        public static void main(String[] args) throws Exception {
            ServerBai1 ser= new ServerBai1(7777);
            ser.run();
}
}


Bookmark and Share

0 comments:

Post a Comment

Next previous home

Cộng đồng yêu thiết kế Việt Nam Thiet ke website, danang