Klient dasturi import com.sun.net.ssl.internal.ssl.Provider;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.security.Security;
import java.util.Scanner;
public class Client {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
//Serverning mijozlar ulanishini qabul qiladigan port raqami
int serverPort = 7712;
//Server manzili
String serverName = "localhost";
/*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());
//Server sertifikati va ommaviyligini o'z ichiga olgan trustStore faylini belgilash
System.setProperty("javax.net.ssl.trustStore", "myTrustStore.jts");
//trustStore faylining parolini belgilash
System.setProperty("javax.net.ssl.trustStorePassword", "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 {
//SSLSocketFactory ssl kontekstini o'rnatadi va SSLSocketni yaratadi
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
//SSLServerFactory allaqachon o'rnatilgan ssl kontekstidan foydalanib SSLSocket yaratish va serverga ulanish
SSLSocket sslSocket = (SSLSocket) sslsocketfactory.createSocket(serverName, serverPort);
//Serverga xabar yuborish uchun OutputStream yaratish
DataOutputStream outputStream = new DataOutputStream(sslSocket.getOutputStream());
//Server tomonidan yuborilgan xabarlarni o'qish uchun InputStream yaratish
DataInputStream inputStream = new DataInputStream(sslSocket.getInputStream());
//Ulangandan so'ng server tomonidan yuborilgan birinchi xabarni o'qish
System.err.println(inputStream.readUTF());
//Mijoz tomonidan kiritilgan xabarni serverga yuborishda davom etish, agar u "yopish" buyrug'i yuborilmaganda
while (true) {
System.out.println("Serverga yuboriladigan xabarni kiriting : ");
String messageToSend = sc.nextLine();
outputStream.writeUTF(messageToSend);
System.err.println(inputStream.readUTF());
if (messageToSend.equals("yopish")) {
break;
}
}
} catch (Exception ex) {
System.err.println("Xatolikni aniqlash : " + ex.toString());
}
}
}