Professional Documents
Culture Documents
INETADDRESS
INETADDRESS
INETADDRESS
1
Nội dung
IP
DNS
InetAddress
URL
URLConnection
IPv4
3
Địa chỉ IPv4
L ớp Cấu trúc địa chỉ Format Số bit Tổng số Tổng số Vùng địa
IP mạng/số mạng/lớp host/mạng chỉ IP
bit host
E 1|1|1|1 - - - - 240.0.0.0-
254.255.255.
255
Loopba - - - - - 127.x.x.x
ck
DNS: Domain Name System
5
DNS: Domain Name System
Lược đồ tên miền (IANA, ICANN)
Nslookup (Domain/IP)
6
Quản lý tên miền
Mỗi nhóm tên (Zone) có một server chứng thực tên
Mỗi Zone được ủy quyền chứng thực một tập tên miền.
Một Zone
7
Nhóm Zone gốc và Server
13 servers quản lý zone gốc trên toàn cầu
8
Liên kết giữa các sever DNS
9
Ví dụ xác định tên miền
10
Lớp InetAddress
Biểu diễn và quản lý địa chỉ IP:
java.net.InetAddress
Cho phép xác định địa chỉ IP dựa vào tên
miền và ngược lại.
Các lớp mạng khác sử dụng nó để xác định
host ở xa. Ví dụ: Socket,ServerSocket..
Tạo đối tượng InetAddresses
Không có phương thức khởi tạo.
Tất cả các đối tượng địa chỉ được tạo ra, địa
chỉ tương ứng phải được kiểm trả với DNS.
public static InetAddress
InetAddress.getByName(String hostname)
public static InetAddress[]
InetAddress.getAllByName(String hostname)
public static InetAddress
InetAddress.getLocalHost()
Tạo đối tượng InetAddresses
Tạo đối tượng: getByName()
public static InetAddress getByName(String
host) throws UnknownHostException
Tạo đối tượng: getByName()
public static InetAddress getByName(String
host) throws UnknownHostException
import java.net.*;
public class PhanLoaiDCIP
{
public static void main(String[] args) {
try{
if(args.length!=1) System.out.println("Cach su dung: java TimDCIP <Hostname>");
InetAddress host = InetAddress.getByName(args[0]);
String hostName = host.getHostName();
System.out.println("Host name:"+hostName);
System.out.println("Dia chi IP:"+host.getHostAddress());
byte[] b=host.getAddress();
int i=b[0]>=0?b[0]:256+b[0];
if((i>=1)&&(i<=126)) System.out.println(host+" thuoc dia chi lop A");
if((i<=191)&&(i>=128)) System.out.println(host+" thuoc dia chi lop B");
if((i<=223)&&(i>=192)) System.out.println(host+" thuoc dia chi lop C");
}catch(UnknownHostException e){
System.out.println("Khong tim thay dia chi"); return;
}
}}
Ví dụ
import java.net.*;
public class Local {
public static void main(String[] args) {
try {
InetAddress me = InetAddress.getLocalHost();
System.out.println("My name is " + me.getHostName());
System.out.println("My address is " + me.getHostAddress());
byte[] address = me.getAddress();
for (int i = 0; i < address.length; i++) {
System.out.print(address[i] + " ");
}
System.out.println();
}
catch (UnknownHostException e) {
System.err.println("Could not determine local address.");
System.err.println("Perhaps the network is down?");
}
}
} My name is PC
My address is 192.168.1.6
-64 -88 1 6
Phương thức kiểm tra
public boolean isReachable(int timeout)
throws IOException
public boolean isMulticastAddress()
public boolean isSiteLocalAddress()
NetworkInterface
try {
NetworkInterface ni = NetworkInterface.getByName("eth0");
if (ni == null) {
System.err.println("No such interface: eth0" );
}
} catch (SocketException ex) {
System.err.println("Could not list sockets." );
}
20
NetworkInterface
import java.net.*; import java.util.*;
public class InterfaceLister {
public static void main(String[] args) throws Exception {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces( );
while (interfaces.hasMoreElements( )) {
NetworkInterface ni = (NetworkInterface) interfaces.nextElement( );
System.out.println(ni); }
}
}
21
Cổng ứng dụng
Một host có một địa chỉ duy nhất để nhận dạng, làm
thế nào để đồng thời nhiều ứng dụng trên một host có
thể giao tiếp với host khác ?
Cổng-port được sử dụng. Có 65,536 port.
Một dịch vụ tương ứng với một port. Ví dụ, http chạy
với port 80.
Giao thức
Mỗi dịch vụ tương ứng với một giao thức.
Giao thức qui định 2 dịch vụ của 2 host giao
tiếp với nhau như thế nào.
Ví dụ
telnet,SMTP,POP3, HTTP
http://www.faqs.org/rfc/
URL
URL-"Uniform Resource Locator", xác định
tài nguyên trên mạng Internet.
Các thành phần của URL.
Giao thức
Tên miền
Cổng
Đường dẫn
Chuỗi truy vấn
The java.net.URL class
Đối tượng biểu diễn URL
Cho phép kết nối tới server
Có các phương thức
Tạo một đối tượng URL mới
Phân tích các thành phần của URL
Lấy luồng nhập để đọc dữ liệu từ server
URL
Ví dụ URL
http://java.sun.com/
file:///Macintosh%20HD/Java/Docs/JDK
%201.1.1%20docs/api/
java.net.InetAddress.html#_top_
http://www.macintouch.com:80/newsrecent.shtml
ftp://ftp.info.apple.com/pub/
mailto:elharo@metalab.unc.edu
telnet://utopia.poly.edu
ftp://mp3:mp3@138.247.121.61:21000/c%3a/stuff/mp3/
http://elharo@java.oreilly.com/
https://metalab.unc.edu/nywc/comps.phtml?
category=Choral+Works
Quản lý dữ liệu và giao thức
Dữ liệu và giao thức tương ứng được kiểm
soát riêng.
Trình quản lý giao thức giao tiếp với server
ở một kênh riêng. Dữ liệu được truyền tách
biệt ở một luồng nhập riêng InputStream,
ImageProducer.
Tạo đối tượng URL
public URL(String url) throws
MalformedURLException
public URL(String protocol, String host, String
file) throws MalformedURLException
public URL(String protocol, String host, int
port, String file) throws MalformedURLException
public URL(URL u, String s) throws
MalformedURLException
Khi máy ảo tạo ra đối tượng URL, thì đồng thời nó tìm trình
quản lý giao thức tương ứng ví dụ “http” hoặc “mailto”.
Nếu không có, hàm khởi tạo ném ra ngoại lệ
MalformedURLException.
Ví dụ
try {
URL u = new
URL("http://www.poly.edu/fall97/grad.html#cs");
}
catch (MalformedURLException e) {}
Ví dụ
URL u = null;
try {
u = new URL("http", "www.poly.edu",
"/schedule/fall97/bgrad.html#cs");
}catch (MalformedURLException e) {}
Ví dụ
URL u = null;
try {
u = new URL("http", "www.poly.edu", 8000,
"/fall97/grad.html#cs");
}
catch (MalformedURLException e) {}
Ví dụ
try {
URL u1 = new
URL("http://metalab.unc.edu/index.html");
URL u2 = new URL(u1, ”books.html");
}
catch (MalformedURLException e) {}
Thao tác thành phần URL
public String getProtocol()
public String getHost()
public int getPort()
public String getFile()
public String getRef()
public String getQuery()
Ví dụ
try {
URL u = new URL("http://tdtu.edu.vn ");
System.out.println("The protocol is"+u.getProtocol());
System.out.println("The host is " + u.getHost());
System.out.println("The port is " + u.getPort());
System.out.println("The file is " + u.getFile());
System.out.println("The anchor is " + u.getRef());
System.out.println("The query is " + u.getQuery());
}catch (MalformedURLException e) { }
int getConnectionTimeout() trả về cài đặt cho thời gian chờ kết nối.
Object getContent(Class[] classes) truy xuất nội dung của kết nối URL.
String getContentEncoding() trả về giá trị của trường tiêu đề mã hóa nội dung.
URLConnection Class
Cú pháp
public URLConnection openConnection()throws IOException{}
-
Đọc dữ liệu từ server
public final InputStream openStream()
throws IOException
public URLConnection openConnection()
throws java.io.IOException
public final Object getContent() throws
java.io.IOException
Ví dụ
HttpURLConnection class
Lớp Java HttpURLConnection chỉ hoạt động với giao thức HTTP
Với sự trợ giúp của lớp HttpURLConnection, bạn có thể truy xuất thông tin của
bất kỳ URL HTTP nào như thông tin tiêu đề, mã trạng thái, mã phản hồi, v.v.
Java.net.HttpURLConnection là lớp con của lớp URLConnection.
HttpURLConnection class
Phương thức khởi tạo:
protected HttpURLConnection(URL u)
HttpURLConnection class
Các phương thức kết nối HttpURL trong Java:
HttpURLConnection class
Cách lấy đối tượng của lớp HttpURLConnection