MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
“Telekommunikatsiya texnologiyalari” fakulteti
FAN: Tarmoqni dasturlash asoslari
Laboratoriya ishi - 10
Talaba guruh:416-20
Bajardi: Sayipov Sharifjon
Tekshirdi: Abdullaev Ulug’bek
Toshkent 2023
10-LABORATORIYA ISHI
Mavzu:Tarmoqda xavfsiz soketlar (Secure Sockets)larni yaratish
Ishning maqsadi: Talabalarda Java dasturlash tilining javax.net.* va javax.net.ssl.* paketlari klasslaridan foydalanib tarmoqda xavfsiz soketlarlarni yaratish bo‘yicha amaliy ko‘nikmalar hosil qilish.
Undan keyin Win+R tugmasini bosamiz, keyin cmd komandasini kiritamiz va bizda quyidagi oyna ochiladi
Server dasturi
import com.sun.net.ssl.internal.ssl.Provider;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.security.Security;
public class Server {
public static void main(String args[]) {
//Ushbu server mijoz ulanishlarini qabul qiladigan port raqami
int port = 7712;
/*SSL va TLS protokollarini ta'minlovchi JSSE (Java Secure Socket Extension) provayderini qo'shish
va ma'lumotlarni shifrlash, server autentifikatsiyasi, xabarlar yaxlitligi uchun funksionallikni
va ixtiyoriy mijoz autentifikatsiyasini o'z ichiga oladi.*/
Security.addProvider(new Provider());
//sertifikat va pubic(ochiq) kalit va shaxsiy kalitni o'z ichiga olgan keyStore faylini belgilash
System.setProperty("javax.net.ssl.keyStore", "myKeyStore.jks");
//keyStore faylining parolini belgilash
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
//Bu qism server asosiy ishchi holatga o'tganda o'chirib yuboriladi. Faqat ma'lumotlar shifrli holatda ekanini
//tekshirib turish uchun kerak
System.setProperty("javax.net.debug","all");
try {
//SSLServerSocketFactory ssl kontekstini o'rnatadi va SSLServerSocket-ni yaratadi
SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
//SSLServerSocketFactory o'rnatilgan ssl kontekstidan foydalanib SSLServerSocket yaratish
SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketfactory.createServerSocket(port);
System.out.println("Server ishga tushdi va mijoz ulanishini qabul qilishga tayyor");
//SSL mijozi ushbu serverga ulanishini kutmoqda
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
//Mijoz tomonidan yuborilgan xabarlarni qabul qilish uchun InputStream ni yaratish
DataInputStream inputStream = new DataInputStream(sslSocket.getInputStream());
//Mijozga xabar yuborish uchun OutputStream yaratish
DataOutputStream outputStream = new DataOutputStream(sslSocket.getOutputStream());
outputStream.writeUTF("Salom mijoz, biror ma'lumot yozishingiz uchun tayyormiz!");
//Mijoz "yopish" so'zini yubormaguncha, siz olgan xabarni yuborishda davom etadi
while (true) {
String recivedMessage = inputStream.readUTF();
System.out.println("Mijozdan kelgan ma'lumot : " + recivedMessage);
if (recivedMessage.equals("yopish")) {
outputStream.writeUTF("Xayr, aloqa tugatildi");
outputStream.close();
inputStream.close();
sslSocket.close();
sslServerSocket.close();
break;
} else {
outputStream.writeUTF("Siz ushbu xabarni jo'natdingiz : " + recivedMessage);
}
}
} catch (Exception ex) {
System.err.println("Xatolikni aniqlash : " + ex.toString());
}
}
}
Do'stlaringiz bilan baham: |