Topshiriq:
Talaba laboratoriya ishi uchun shaxsiy topshiriq oladi. Bu topshiriq bo‘yicha
talaba tarmoqda xavfsiz soketlarlarni yaratadi.
Ishni bajarish tartibi:
1-qadam. Ish NetBeans IDE integrallashgan ishlab chiqarish muhitini
yuklash bilan boshlanadi. Quyidagi rasmda NetBeans IDE muhitining asosiy
darchasi yuklangan.
5
10.1-rasm. NetBeans IDE muhiti asosiy darchasi
2-qadam.
“Файл”
menyusining
“Открыть
проект”
bo‘limidan
“Talabaning_Ismi” loyihasi ochiladi.
10.2-rasm. NetBeans IDE muhiti yangi klass yaratish
3-qadam. “New Класс Java” darchasining “Имя класса” maydoniga
“Client”, “Пакет” maydoniga “ssl” kiritilib “Готово” tugmasi bosiladi.
6
10.3-rasm. NetBeans IDE muhiti “New Класс Java” darchasi
4-qadam. Yaratilgan Client.java fayliga quyidagi Java kodi kiritiladi.
package ssl;
import java.io.*;
import javax.net.ssl.*;
import com.sun.net.ssl.*;
import com.sun.net.ssl.internal.ssl.Provider;
import java.security.Security;
public class Client {
public static void main(String[] args) throws Exception{
String strServerName = "localhost"; // SSL Server nomi
int intSSLport = 4443; // SSL eshitadigan port raqami
PrintWriter out = null;
BufferedReader in = null;
{
// JSSE provayderni ro’yxatdan o’tkazish
Security.addProvider(new Provider());
}
try {
// Kliyent soketini yaratish
7
SSLSocketFactory
sslsocketfactory
=
(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket
sslSocket
=
(SSLSocket)sslsocketfactory.createSocket(strServerName,intSSLport);
// Server bilan aloqa qiladigan oqimlarni yaratish
out = new PrintWriter(sslSocket.getOutputStream(), true);
in
=
new
BufferedReader(new
InputStreamReader(sslSocket.getInputStream()));
BufferedReader
stdIn
=
new
BufferedReader(new
InputStreamReader(System.in));
String userInput = "Salom ";
out.println(userInput);
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.println(userInput);
// Closing the Streams and the Socket
out.close();
in.close();
stdIn.close();
sslSocket.close();
}
catch(Exception exp)
{
System.out.println(" Xatolik ro'y berdi .... " +exp);
exp.printStackTrace();
}
}
}
3- va 4- qadamlar qaytadan bajarilib Server.java fayli yaratiladi.
package ssl;
import java.io.*;
import java.security.Security;
import java.security.PrivilegedActionException;
8
import javax.net.ssl.*;
import com.sun.net.ssl.*;
import com.sun.net.ssl.internal.ssl.Provider;
public class Server {
public static void main(String[] args) throws Exception{
int intSSLport = 4443; // Kliyentlarni tinglaydigan port raqami
{
// JSSE provayderni ro’yxatdan o’tkazish
Security.addProvider(new Provider());
//Specifying the Keystore details
System.setProperty("javax.net.ssl.keyStore","testkeystore.ks");
System.setProperty("javax.net.ssl.keyStorePassword","123456");
}
try {
// Server soketini yaratish
SSLServerSocketFactory
sslServerSocketfactory
=
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket
sslServerSocket
=
(SSLServerSocket)sslServerSocketfactory.createServerSocket(intSSLport);
SSLSocket
sslSocket
=
(SSLSocket)sslServerSocket.accept();
// Kliyent bilan aloqa qiladigan kiritish/chiqarish
oqimlarini yaratish
while(true)
{
PrintWriter
out
=
new
PrintWriter(sslSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
sslSocket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
System.out.println(inputLine);
}
9
// Oqimlar va soketni yopish
out.close();
in.close();
sslSocket.close();
sslServerSocket.close();
}
}
catch(Exception exp)
{
PrivilegedActionException
priexp
=
new
PrivilegedActionException(exp);
System.out.println(" Priv exp --- " + priexp.getMessage());
System.out.println(" Xatolik ro'y berdi .... " +exp);
exp.printStackTrace();
}
}
}
10.4-rasm. NetBeans IDE muhiti
Nazorat savollari
1. Java Security kutubxonasi
2. Java Secure Socket Extension (JSSE)
3. JSSE standart API
10
4. SSLSocket & SSLEngine yaratish
5. Java Security kutubxonasi
6. Datagram Transport Layer Security
7. Transport Layer Security
8. Secure Socket Layer
9. javax.net paketi
10. javax.net.ssl paketi
|