1-Amaliy ish. 431-21 guruh talabasi: Keldiyev Axror toshkent 2023. Nazariy qism
Download 34.92 Kb.
|
axror 1
- Bu sahifa navigatsiya:
- Kliyent soketini yaratish
O’ZBEKISTON RESPUBLIKASI RAQAMLI TEXNOLOGIYALAR VAZIRLIGI MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI. INFOKOMMUNIKATSIYADA DASTURLASH FANIDAN 1-Amaliy ish. 431-21 guruh talabasi:Keldiyev Axror TOSHKENT 2023. 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. 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. package klient.Lab1; import java.net.InetAddress; import java.io.*; import java.net.*; import java.util.Arrays; public class TCPServer { public static void main(String[] args) { int serverPort = 12345; try (ServerSocket serverSocket = new ServerSocket(serverPort)) { System.out.println("Server " + InetAddress.getLocalHost() + ":" + serverPort + " portda eshitishni boshladi..."); while (true) { try (Socket clientSocket = serverSocket.accept()) { System.out.println("Klient " + clientSocket.getInetAddress() + " bog'landi."); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); int n = Integer.parseInt(in.readLine()); int[] numbers = new int[n]; for (int i = 0; i < n; i++) { numbers[i] = Integer.parseInt(in.readLine()); } // Sonlarni o'sish tartibida saralash Arrays.sort(numbers); // Saralgan sonlarni klientga yuborish for (int number : numbers) { out.println(number); } } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } } .................................................................................... package klient.Lab1; import java.util.Scanner; import java.io.*; import java.net.*; public class TCPClient { public static void main(String[] args) { String serverHost = "127.0.0.1"; int serverPort = 12345; try (Socket clientSocket = new Socket(serverHost, serverPort)) { System.out.println("Serverga bog'landi."); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); Scanner scanner = new Scanner(System.in); System.out.print("N sonini kiriting: "); int n = scanner.nextInt(); out.println(n); for (int i = 0; i < n; i++) { System.out.print((i + 1) + "-sonni kiriting: "); int num = scanner.nextInt(); out.println(num); } // Saralgan sonlarni o'qish va chiqarish for (int i = 0; i < n; i++) { int sortedNumber = Integer.parseInt(in.readLine()); System.out.println("Saralangan " + (i + 1) + "-son: " + sortedNumber); } } catch (IOException e) { e.printStackTrace(); } } } Download 34.92 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling