1-amaliy mashg’ulot. Mavzu: tcp kliyent tarmoq dasturini amaliy o‘rganish. Ishning maqsadi
Download 1.94 Mb.
|
Amaliyot ishlari
public static void main(String args[]) throws Exception
{ DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; System.out.println("Server ishga tushdi!"); while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String( receivePacket.getData()); System.out.println("Qabul qilindi: " + sentence); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } } Ko‘rib chiqilgan kodda Server klass konstruktorida DatagramSocket klassining socket ob’ekti yaratiladi. Soket 9876 portga initsializatsiya qilingandan so‘ng, klient/server o‘zaro aloqasini boshqaruvchi receive() va send() metodlari chaqiriladi. 4.1-jadval Topshiriq variantlari
Download 1.94 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling