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
Do'stlaringiz bilan baham: |