www.sites.google.com/site/khaiphong
Giáo viên: Dương Khai Phong
Email:
Lý thuyết: 45 tiết
Thực hành: 30 tiết
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
Nội dung mơn học
1
Tổng quan lập trình java
2
Lập trình hướng đối tượng
3
Lập trình giao diện đồ họa
4
Lập trình mạng cơ bản
5
Ơn tập
ĐH Cơng nghệ Thơng tin
www.sites.google.com/site/khaiphong
LẬP TRÌNH MẠNG CĂN BẢN
A
Giới thiệu lập trình mạng
B
Các lớp hỗ trợ lập trình mạng Socket
C
TCP Socket Programming
D
UDP Socket Programming
E
Remote Method Invocation (RMI)
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
LẬP TRÌNH MẠNG CĂN BẢN
A
Giới thiệu lập trình mạng:
Các khái niệm liên quan đến lập trình mạng:
Mạng máy tính
Ports
Sockets và Ports
Mơ hình Client – Server
Socket Programming
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
LẬP TRÌNH MẠNG CĂN BẢN
A
Giới thiệu lập trình mạng:
Các khái niệm liên quan đến lập trình mạng:
Mạng máy tính
Ports
Sockets và Ports
Mơ hình Client – Server
Socket Programming
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Mạng máy tính:
Mạng máy tính là tập hợp các máy tính hoặc các thiết bị
được nối với nhau bởi các đường truyền vật lý và theo
một kiến trúc nào đó.
Tầng ứng dụng
Standard apps
• HTTP
• FTP
• Telnet
User apps
Tầng vận chuyển
TCP
UDP
Programming Interface:
• Sockets
Tầng mạng
IP
Tầng kết nối
Device drivers
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Mạng máy tính:
Application
(http,ftp,telnet,…)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
TCP (Tranmission Control
Protocol) là giao thức vận
chuyển dựa trên kết nối logic
trực tiếp giữa 2 máy tính.
Ví dụ:
HTTP
FTP
Telnet
ĐH Cơng nghệ Thơng tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Mạng máy tính:
Application
(http,ftp,telnet,…)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
UDP
(User
Datagram
Protocol) là giao thức vận
chuyển phi kết nối thực hiện
gửi các packets
gọi là
datagrams từ máy này sang
máy khác.
Ví dụ:
Clock server
Ping
ĐH Cơng nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Ports
Ports:
TCP và UDP sử dụng port để xác định chương trình sẽ
nhận dữ liệu khi đến 1 máy tính.
server
P
o
r
t
TCP
app
app
app
app
port
port
port
port
Client
TCP or UDP
Packet
Data
port#
data
ĐH Cơng nghệ Thơng tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Ports
Ports:
Port là 1 con số nguyên
Một số port đã được dành riêng cho các dịch vụ mạng
phổ biến:
ftp 21/tcp
telnet 23/tcp
smtp 25/tcp
login 513/tcp
Nên dùng giá trị lớn hơn 1024
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Ports
Sockets và Ports
socket
any port
agreed port
socket
message
client
server
other ports
Internet address = 138.37.94.248
Internet address = 138.37.88.249
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Ports
Sockets và Ports
Mơ hinh Client và Server
Server machine
Client
Server
Network
Client machine
a client, a server, and network
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
A
Giới thiệu lập trình mạng:
Mạng máy tính
Ports
Sockets và Ports
Mơ hinh Client và Server
Socket Programming
Socket :
Điểm giao tiếp giữa máy tính và mạng
Được dùng để gửi và nhận dữ liệu
Được hỗ trợ bỡi nhiều hệ thống và ngơn
ngữ lập trình
Phân loại: Server (passive) socket,
client (active) socket và UDP socket
sockets (end points)
logical connection
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
B
Các lớp hỗ trợ lập trình mạng Socket:
Gói java.net chứa tất cả các lớp cần thiết cho việc lập
trình mạng (Java Sockets Programming)
Cú pháp khai báo:
import java.net.*;
Các lớp cơ bản trong gói java.net:
InetAddress
Socket
ServerSocket
DatagramSocket
DatagramPacket
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
B
Các lớp hỗ trợ lập trình mạng Socket:
Lớp InetAddress:
Công dụng:
Biểu diễn một địa chỉ Internet, các địa chỉ IP trong một ứng dụng mạng sử
dụng Java.
Lớp này được sử dụng bởi hầu hết các lớp mạng, bao gồm Socket,
ServerSocket, URL, DatagramSocket, DatagramPacket,…
Cú pháp sử dụng:
Sử dụng phương thức static để tạo ra đối tượng InetAddress.
getByName(String host)
getAllByName(String host)
getLocalHost()
Ví dụ:
InetAddress x = InetAddress.getByName("www.uit.edu.vn");
Ném ra biệt lệ UnknownHostException
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
B
Các lớp hỗ trợ lập trình mạng Socket:
Ví dụ: viết chương trình truy xuất địa chỉ IP
import java.net.InetAddress;
import java.net.UnknownHostException;
public class net_lookup {
public static void main(String[] args) {
String hostname=args[0];
try {
InetAddress a = InetAddress.getByName(hostname);
System.out.println(hostname + ":" + a.getHostAddress());
}
catch (UnknownHostException e) {
System.out.println("No address found for " + hostname);
}
}
}
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
Các đối tượng cơ bản trong lập trình mạng TCP Socket
Server sockets
• Chờ kết nối từ mạng
• Tạo ra từ lớp java.net.ServerSocket
Client sockets
• Dùng để gửi và nhận dữ liệu
• Tạo ra từ lớp java.net.Socket
server socket
connection request
client socket
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
Tạo và sử dụng Server Socket
Type
Description
Constructors
ServerSocket(int port) khởi tạo đối tượng
Methods
accept()
chờ yêu cầu kết nối và tạo socket
close()
dừng việc chờ yêu cầu kết nối
server socket
connection request
client socket
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
B
Các lớp hỗ trợ lập trình mạng Socket:
Ví dụ: tạo ServerSocket
try {
ServerSocket server = new ServerSocket(8888);
while (true) {
Socket incoming = server.accept(); // obtain a client socket
// handle client request by reading from and writing to the
socket …
}
} catch (IOException e) {
// handle exception on creating a server socket
}
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
Tạo và sử dụng Client Socket
Type
Description
Tạo client sockets
Phía client:
Socket(String host, int port)
Phía server:
accept() của lớp ServerSocket
khởi tạo đối tượng
Methods
getInputStream()
trả về InputStream để nhận dữ liệu
getOutputStream()
trả về OutputStream để gửi dữ liệu
close()
đóng kết nối
ĐH Cơng nghệ Thơng tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
Mơ hình truyền / nhận dữ liệu giữa Server và Client:
Server
ServerSocket(1234)
Client
Output/write stream
Socket(“128.250.25.158”, 1234)
Input/read stream
Có thể là tên máy
ĐH Cơng nghệ Thơng tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
Ví dụ 1: viết chương trình cho server nhận thơng tin là một chuỗi
ký tự từ phía client gởi lên và chuyển đổi chuỗi này sang chữ hoa
và gởi trả về lại cho phía client dùng phương thức TCP Socket
Client
Server (running on hostid)
create socket, port=x, for incoming request:
welcomeSocket = ServerSocket()
wait for incoming connection request
connectionSocket = welcomeSocket.accept()
TCP
connection setup
create socket,connect to hostid, port=x
clientSocket = Socket()
read request from connectionSocket
send request clientSocket
write reply to connectionSocket
read reply from clientSocket
close connectionSocket
close clientSocket
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
import java.io.*; import java.net.*;
TCP_MYSERVER.JAVA
public class tcp_myserver{
public static void main(String args[])
{ try{ //1. creating a server socket
ServerSocket sk = new ServerSocket(6789,10);
//2. Wait for connection
System.out.println("Waiting for connection");
Socket s = sk.accept();
System.out.println("Connection received from " + s.getInetAddress().getHostName());
//3. get Input and Output streams
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
<< XỬ LÝ DATA NHẬN ĐƯỢC TỪ CLIENT VA TRUYỀN LAI CHO CLIENT>>
//4: Closing connection
in.close();out.close();sk.close();
}
catch(IOException ioException){ ioException.printStackTrace();}
}
}
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
import java.io.*; import java.net.*;
TCP_MYSERVER.JAVA
public class tcp_myserver{
public static void main(String args[])
// Process in/out data from client
{ try{ //1. creating a server socket
String message;
ServerSocket sk = new ServerSocket(6789,10);
// get data from client
//2. Wait for connection
System.out.println("Waiting for connection"); message = in.readLine();
System.out.println("<client> "+message);
Socket s = sk.accept();
System.out.println("Connection received from//" send
+ s.getInetAddress().getHostName());
data to client
//3. get Input and Output streams
out.print(message.toUpperCase());
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.flush(); // flush data to client
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
<< XỬ LÝ DATA NHẬN ĐƯỢC TỪ CLIENT VA TRUYỀN LAI CHO CLIENT>>
//4: Closing connection
in.close();out.close();sk.close();
}
catch(IOException ioException){ ioException.printStackTrace();}
}
}
ĐH Công nghệ Thông tin
www.sites.google.com/site/khaiphong
TỔNG QUAN LẬP TRÌNH JAVA
C
TCP Socket Programming:
import java.io.*; import java.net.*;
public class tcp_myclient{
public static void main(String args[])
{try{ //1. creating a socket to connect to the server
Socket s = new Socket("localhost", 6789);
System.out.println("Connected to localhost in port 6789");
//2. get Input and Output streams
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//3: Communicating with the server
<< XỬ LÝ DATA NHẬN ĐƯỢC TỪ SERVER VA TRUYỀN LAI CHO SERVER>>
//4: Closing connection
in.close(); out.close(); s.close();
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){ ioException.printStackTrace();}
}
TCP_MYCLIENT.JAVA
}
ĐH Công nghệ Thông tin