|
Kompyuter injiniring fakulteti 716-20 guruh talabasi muydinov vasliddinnig
|
bet | 4/6 | Sana | 20.02.2024 | Hajmi | 381,38 Kb. | | #159368 |
Bog'liq 1-dedline Tarmoqni dasturlash2-amaliy ish.
UDP klient dasturini amaliy o’rganish. UDP server tarmoq dasturini amali o’rganish
UDP klienti UDP protokolidan serverga so‘rovlarni yuborish va server ilovasidan javoblarni qabul qilish uchun foydalanadigan ilovadir. Foydalanuvchining UDP-ilovasida UDP serveridan xabarlarni qabul qiladigan DatagramSocket klass ob’ektini yaratish zarur, bu uchun esa quyidagilarni bajarish lozim bo‘ladi:
Server bilan ulanish o‘rnatish uchun DatagramSocket klass ob’ektidan foydalanuvchi soketini yaratish.
DatagramPacket klass ob’ektini yaratish va xabarlarni serverga yuborish uchun send() metodidan foydalanish.
DatagramPacket klass ob’ektini yaratish va serverdan yuborilgan xabarlarni qabul qilish uchun receive() metodidan foydalanish.
Quyidagi kod fragmentidan klient ilovasi uchun DatagramSocket klass ob’ektini yaratish uchun foydalanish mumkin:
DatagramSocket clientSocket = new DatagramSocket();
Oldingi kod fragmentida konstruktor DatagramSocket klass ob’ektini har qanday ruxsat etilgan lokal port bilan bog‘laydi, chunki parametrda port nomeri ko‘rsatilmagan bo‘ladi.
Ushbu DatagramSocket ob’ekti so‘rov yuboriladigan serverning IP-manzili va port raqamini o‘z ichiga oladi. Quyidagi kod fragmenti DatagramPacket ob’ektini berilgan serverga yuborish uchun ishlatiladi:
InetAddress IPAddress = InetAddress.getByName("localhost");
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, 9876);
clientSocket.send(sendPacket);
Oldingi kod fragmetida 4 ta parametrni qabul qiluvchi DatagramPacket konstruktori yordamida packet ob’ekti yaratiladi. DatagramSocket klassining send() metodi DatagramPacket klass ob’ektini serverga yuboradi.
Quyidagi kod fragmentidan serverdan datagramma paketlarini qabul qiluvchi DatagramPacket ob’ektini yaratishda foydalanish mumkin:
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
Oldingi kod fragmentida receive() metodini chaqiruvchi DatagramPacket klassning packet ob’ekti yaratiladi. getData() metodi packet ob’ektidan ma’lumotlarni qabul qiladi va ularni string turdagi o‘zgaruvchida saqlaydi.
Quyidagi kod UDP serverga xabarlarni uzatuvchi va qabul qiluvchi UDP Client klassini yaratish uchun ishlatiladi:
package udp;
import java.io.*;
import java.net.*;
class Client
{
public static void main(String args[]) throws Exception
{
System.out.println("Client ishga tushdi!");
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("localhost"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, 9876); clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length); clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData()); System.out.println("SERVERDAN:" + modifiedSentence); clientSocket.close();
|
| |