10-Laboratoriya ishi Tarmoqda xavfsiz soketlar (Secure Sockets)larni yaratish



Yüklə 1,33 Mb.
səhifə1/2
tarix19.12.2023
ölçüsü1,33 Mb.
#185491
  1   2
tarmoq10


10-Laboratoriya ishi
Tarmoqda xavfsiz soketlar (Secure Sockets)larni yaratish






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());
}
}
}


Yüklə 1,33 Mb.

Dostları ilə paylaş:
  1   2




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin