Package azamjon; import java io. BufferedReader



Download 426,07 Kb.
Sana15.04.2022
Hajmi426,07 Kb.
#554259
Bog'liq
Azamjon 2 lab


package azamjon;

import java.io.BufferedReader;


import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class UDPClient


{
public static void main(String args[]) throws Exception
{
System.out.println("Kliyent 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];
int[] numbers = getNumbersFromUser(inFromUser);
sendData = convertIntArrayToStringByte(numbers);
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
System.out.println("Kiritilgan sonlar serverga jo'natildi");
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
modifiedSentence = modifiedSentence.substring(0, modifiedSentence.indexOf("-"));
System.out.println("O'rta arifmetik qiymat: " + modifiedSentence);
clientSocket.close();
System.out.println("Kliyent soketi yopildi!");
}

private static byte[] convertIntArrayToStringByte(int[] numbers) {


StringBuilder stringData = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
if (i < numbers.length - 1) {
stringData.append(numbers[i]).append(",");
} else {
stringData.append(numbers[i]).append("-");
}
}
return stringData.toString().getBytes();
}

private static int[] getNumbersFromUser(BufferedReader inFromUser) throws IOException {


System.out.println("Serverga nechta son jo'natmoqchisiz?");
int numLength = 0;
try {
numLength = Integer.parseInt(inFromUser.readLine());
} catch (NumberFormatException e) {
System.err.println("Butun son kiriting");
}
System.out.println("Sonlarni kiriting");
int[] numbers = new int[numLength];
for (int i = 0; i < numLength; i++) {
try {
numbers[i] = Integer.parseInt(inFromUser.readLine());
} catch (NumberFormatException e) {
System.err.println("Butun son kiriting");
}
}
return numbers;
}
}
UDP Server

package azamjon;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

class UDPServer


{
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 numbersString = new String( receivePacket.getData());
numbersString = numbersString.substring(0, numbersString.indexOf("-"));
System.out.println();
System.out.println("Kliyentdan qabul qilingan sonlar: " + numbersString);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
int[] numbers = convertIntArrayStringToArray(numbersString);
int[] evenNumbers = getEvenNumbers(numbers);
sendData = convertIntArrayToStringByte(evenNumbers);
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
System.out.println("Server qabul qilingan so'rovni qayta ishlab kliyentga jo'natdi");
}

}

private static byte[] convertIntArrayToStringByte(int[] numbers) {


StringBuilder stringData = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
if (i < numbers.length - 1) {
stringData.append(numbers[i]).append(",");
} else {
stringData.append(numbers[i]).append("-");
}
}
return stringData.toString().getBytes();
}

private static int[] getEvenNumbers(int[] numbers) {


int sum = 0;
int leng = numbers.length;
List evenNumbers = new ArrayList<>();
for (int number : numbers) {
sum += number;
}
evenNumbers.add(sum/leng);

return evenNumbers.stream().mapToInt(i -> i).toArray();


}

private static int[] convertIntArrayStringToArray(String numbersString) {


String[] stringNumbers = numbersString.split(",");
int[] numbers = new int[stringNumbers.length];
for (int i = 0; i < stringNumbers.length; i++) {
try {
numbers[i] = Integer.parseInt(stringNumbers[i]);
} catch (NumberFormatException e) {
System.err.println("Butun son kiriting");
}
}
return numbers;
}
}
natija


Download 426,07 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish