Bajardi: Abdurahmonov Jasurbek
Download 20.41 Kb.
|
1-labaratoriya
1-labaratoriya Bajardi: Abdurahmonov Jasurbek 1-LABORATORIYA ISHI Mavzu: TCP kliyent-server tarmoq dasturini yaratish. Ishning maqsadi: Talabalarda Java dasturlash tili java.net.* va java.io.* paketlari klasslaridan foydalanib TCP kliyent-server tarmoq dasturini yaratish bo‘yicha amaliy ko‘nikmalar hosil qilish. Nazariy qism: TCP kliyentini yaratish. Kliyentning birinchi o‘rinda bajaradigan vazifasi server bilan aloqa o‘rnatishdan iborat. Server va kliyent o‘rtasida aloqa o‘rnatish uchun Socket obyekti yaratiladi. TCP kliyent soketi ilovasini yaratish uchun quyidagi vazifalar bajariladi: 1. Socket obyektidan foydalanib kliyent soketini yaratish. 2. Soketga yozish va undan o‘qish. 3. Ulanishni tugallash. Kliyent soketini yaratish. Kliyent soketining obyekti server tomonidan eshitiladigan ikki parametr IP manzil va port raqamini qabul qiluvchi Socket klassining konstruktori yordamida yaratiladi. Socket clientSocket = new Socket("127.0.0.1", 1001); Yuqoridagi kod fragmentida 127.0.0.1 ga teng bo‘lgan IP manzil va 1001- port shunday soketni aniqlaydiki, ushbu soketda server kliyent so‘rovlarini kutadi. Soketdan o‘qish va yozish. Kliyent va server o‘rtasida ulanish o‘rnatilganidan so‘ng kliyent serverga soket orqali so‘rov yuboradi. Soketdan o‘qish va yozish fayldan o‘qish va yozishga o‘xshash. Kliyentga server bilan o‘zaro aloqada ishlashni ta’minlash uchun quyidagilarni bajarish zarur: PrintStream va BufferedReader klasslari uchun ikkita obyekt e’lon qilinadi. Ushbu obyektlar soketdan o‘qish va yozish uchun ishlatiladi. DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 2 Socket klassining getInputStream() va getOutputStream() metodlari kliyentga server bilan o‘zaro aloqada ishlash imkonini beradi. getInputStream() metodi BufferedReader obyektiga soketdan o‘qish, getOutputStream() metodi esa DataOutputStream obyektiga soketga yozish imkonini beradi. Kliyent ilovasida kiritilgan ma’lumotlar serverga uzatilishi uchun standart kirish bilan aloqa o‘rnatish uchun BufferdReader klassining yana bir obyekti e’lon qilinadi. Quyidagi kod fragmenti konsol oynasidan ma’lumotlarni o‘qish uchun ishlatiladi: BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Keltirilgan kod fragmenti foydalanuvchiga klaviaturadan ma’lumotlarni kiritish imkonini beradi. 4-variant: Ixtiyoriy n ta sonning o‘rta geometrigini hisoblash package uz.tatu; import java.io.*; import java.net.*; import java.util.Scanner; public class TCPClient { public static void main(String[] args) { String serverAddress = "localhost"; // Change this to the server's IP or hostname int serverPort = 12345; // Change this to the server's port number try (Socket socket = new Socket(serverAddress, serverPort); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); Scanner scanner = new Scanner(System.in)) { System.out.print("Enter numbers separated by spaces: "); String numbers = scanner.nextLine(); out.println(numbers); String response = in.readLine(); double geometricMean = Double.parseDouble(response); System.out.println("Geometric mean of the numbers: " + geometricMean); } catch (UnknownHostException e) { System.err.println("Unknown host: " + serverAddress); } catch (IOException e) { e.printStackTrace(); } } } Abror, [21.10.2023 14:07] package uz.tatu; import java.io.*; import java.net.*; public class TCPServer { public static void main(String[] args) { int port = 12345; // Change this to your desired port number try (ServerSocket serverSocket = new ServerSocket(port)) { System.out.println("Server is listening on port " + port); while (true) { try (Socket clientSocket = serverSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) { String inputLine = in.readLine(); String[] numberStrings = inputLine.split(" "); double[] numbers = new double[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Double.parseDouble(numberStrings[i]); } double geometricMean = calculateGeometricMean(numbers); out.println(geometricMean); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } // Helper method to calculate the geometric mean of an array of numbers private static double calculateGeometricMean(double[] numbers) { if (numbers.length == 0) { throw new IllegalArgumentException("Empty array"); } double product = 1.0; for (double number : numbers) { product *= number; } return Math.pow(product, 1.0 / numbers.length); } } Download 20.41 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling