b07 ltm lap trinh socket voi ssl
TRANSCRIPT
![Page 1: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/1.jpg)
Lập trình mạng Lập trình Socket với SSL
Giảng viên: TS. Nguyễn Mạnh HùngHọc viện Công nghệ Bưu chính Viễn thông (PTIT)
![Page 2: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/2.jpg)
2
Nội dung
Giao thức TCP/IP với SSL Cài đặt phía server Cài đặt phía client Ví dụ: đảo ngược chuỗi Bài tập
![Page 3: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/3.jpg)
3
TCP/IP trong mô hình ISO
[image source: http://1.bp.blogspot.com] [image source: http://jan.newmarch.name]
![Page 4: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/4.jpg)
4
Giao thức TCP/IP
![Page 5: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/5.jpg)
5
Server (1)
Bước 1: Mở một server socket tại một cổng có số hiệu xác định
try { SSLServerSocketFactory sslserversocketfactory =
SSLServerSocketFactory.getDefault(); SSLServerSocket sslserversocket = sslserversocketfactory.createServerSocket(9999);}catch(IOException e) { System.out.println(e);}
![Page 6: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/6.jpg)
6
Server (2)
Bước 2: Tạo một đối tượng socket từ ServerSocket để lắng nghe và chấp nhận các kết nối từ phía client
try { SSLSocket sslsocket = sslserversocket.accept(); DataInputStream is = new
DataInputStream(sslsocket.getInputStream()); PrintStream os = new
PrintStream(sslsocket.getOutputStream());}catch (IOException e) {
System.out.println(e);}
![Page 7: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/7.jpg)
7
Server (3)
Bước 3: Mỗi khi nhận được dữ liệu từ client, tiến hành xử lí và gửi trả về client đó
// Xu li du lieu nhan duoc va tra ve while (true) { // doc du lieu vao String input = is.readUTF(); // xu li du lieu ... // tra ve du lieu os.println(dữ liệu trả về); }
![Page 8: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/8.jpg)
8
Client (1)
Bước 1: Mở một kết nối client socket đến server có tên xác định, tại một cổng có số hiệu xác định
try { SSLSocketFactory sslsocketfactory =
SSLSocketFactory.getDefault(); SSLSocket sslsocket =
sslsocketfactory.createSocket("localhost", 9999); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
![Page 9: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/9.jpg)
9
Client (2)
Bước 2: Mở luồng kết nối vào (nhận dữ liệu) và kết nối ra (gửi dữ liệu) đến socket vừa mở
try { DataOutputStream os = new DataOutputStream(sslsocket.getOutputStream()); DataInputStream is = new DataInputStream(sslsocket.getInputStream());} catch (UnknownHostException e) { System.err.println(e);} catch (IOException e) { System.err.println(e);}
![Page 10: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/10.jpg)
10
Client (3)
Bước 3: Gửi dữ liệu đến server
try { os.writeBytes(dữ liệu gửi đi); }catch (UnknownHostException e) { System.err.println("e); } catch (IOException e) { System.err.println("e); }
![Page 11: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/11.jpg)
11
Client (4)
Bước 4: Nhận dữ liệu đã qua xử lí từ server về
try { String responseStr; // du lieu nhan ve if ((responseStr = is.readUTF()) != null) { return responseStr; } }catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
![Page 12: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/12.jpg)
12
Client (5)
Bước 5: Đóng các kết nối tới server
try { os.close(); is.close(); mySocket.close(); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
![Page 13: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/13.jpg)
13
Chạy chương trìnhChạy server:
>java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 <tên file server>
Chạy client:>java -Djavax.net.ssl.trustStore=mySrvKeystore
-Djavax.net.ssl.trustStorePassword=123456 <tên file client>
Muốn debug, thêm tùy chọn này váo cả 2 lệnh trên: -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl
![Page 14: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/14.jpg)
14
Ví dụ: đảo chuỗi (1)import java.lang.String;public class ReverseString {private String _string;// khoi tao khong tham sopublic ReverseString(){}// khoi tao co tham sopublic ReverseString(String _string){
this._string = _string;}public String get_string() {
return _string;}public void set_string(String _string) {
this._string = _string;}
![Page 15: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/15.jpg)
15
Ví dụ: đảo chuỗi (2)
//phuong thuc dao nguoc chuoi ki tu cua lop naypublic void reverse(){
String tmp ="";for(int i=0; i < this._string.length();i++)
tmp.concat(_string.substring(i, i+1));this._string = tmp;
}}
![Page 16: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/16.jpg)
16
Ví dụ: đảo chuỗi – server (1)import javax.net.ssl.SSLServerSocket;import javax.net.ssl.SSLSocket;public class TCPServer {// Khai bao server socket, luong vao-ra, va doi tuong socket SSLServerSocket myServer = null; String input; DataInputStream is; PrintStream os; SSLSocket clientSocket = null; // Mo mot server socketpublic void openServer(){ try {
SSLServerSocketFactory sslserversocketfactory =SSLServerSocketFactory.getDefault();
SSLServerSocket myServer = sslserversocketfactory.createServerSocket(9999); }catch(IOException e) { System.out.println(e); }}
![Page 17: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/17.jpg)
17
Ví dụ: đảo chuỗi – server (2)// Chap nhan ket noi va xu li du lieupublic void listening(){try { clientSocket = myServer.accept(); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); // Xu li du lieu nhan duoc va tra ve while (true) { // doc du lieu vao input = is.readUTF(); // xu li du lieu ReverseString str = new ReverseString(input); str.reverse(); // tra ve du lieu os.println(str.get_string()); }}catch (IOException e) { System.out.println(e);}}}
![Page 18: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/18.jpg)
18
Ví dụ: đảo chuỗi – client (1)import javax.net.ssl.SSLSocket;import javax.net.ssl.SSLSocketFactory;public class TCPClient {// khai bao socket cho client, luong vao-raSSLSocket mySocket = null; DataOutputStream os = null;DataInputStream is = null;// Tao ket noipublic void connection(){
try { SSLSocketFactory sslsf = SSLSocketFactory.getDefault(); SSLSocket mySocket = sslsf.createSocket("hostname", 9999); os = new DataOutputStream(mySocket.getOutputStream()); is = new DataInputStream(mySocket.getInputStream()); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }}
![Page 19: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/19.jpg)
19
Ví dụ: đảo chuỗi – client (2)public void send(String str){ // gui du lieu den server if (mySocket != null && os != null) { try { os.writeBytes(str); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } }}public String receive(){ // nhan du lieu tra ve tu server if (mySocket != null && is != null) { try { String responseStr; if ((responseStr = is.readUTF()) != null) { return responseStr; } } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } } return null;}
![Page 20: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/20.jpg)
20
Ví dụ: đảo chuỗi – client (3)// dong cac ket noipublic void close(){ if (mySocket != null && os != null && is != null) { try { os.close(); is.close(); mySocket.close(); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } }} }
![Page 21: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/21.jpg)
21
Bài tập (1)
Cài đặt theo mô hình giao thức TCI/IP và
SSL cho bài toán: Client yêu cầu người dùng nhập từ bàn
phím hai số nguyên dương a và b server nhận và tính BSCNN của a và b,
sau đó trả về kết quả cho client Client nhận lại kết quả và show ra màn
hình cho người dùng
![Page 22: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/22.jpg)
22
Bài tập (2)
Cùng yêu cầu, nhưng cài đặt đúng mô hình MVC
Cài đặt theo mô hình giao thức TCI/IP và SSL cho bài toán:
Client yêu cầu người dùng nhập từ bàn phím hai số nguyên dương a và b
server nhận và tính BSCNN của a và b, sau đó trả về kết quả cho client
Client nhận lại kết quả và show ra màn hình cho người dùng
![Page 23: b07 LTM Lap Trinh Socket Voi SSL](https://reader034.vdocuments.pub/reader034/viewer/2022042515/553fd7cd550346f4018b48e6/html5/thumbnails/23.jpg)
Questions?