Tarmoqni dasturlash asoslari


Download 426.5 Kb.
bet3/28
Sana03.02.2023
Hajmi426.5 Kb.
#1156818
1   2   3   4   5   6   7   8   9   ...   28
Bog'liq
TDA

TCPClient.java

import java.io.DataInputStream;


import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {


public static void main(String[] args) throws IOException {

Socket socket = new Socket("localhost", 8080);


DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());

Scanner scanner = new Scanner(System.in);


System.out.print("Enter n: ");
int n = scanner.nextInt();

StringBuilder data = new StringBuilder();


for (int i = 0; i < n; i++) {
data.append(scanner.nextInt()).append("~");
}

outputStream.writeUTF(data.toString());

String result = inputStream.readUTF();
System.out.println("Ixtiyoriy n ta sonning o'rta geometrigi: " + result);

socket.close();


inputStream.close();
outputStream.close();

}
}



TCPServer.java

import java.io.DataInputStream;


import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

public class TCPServer {


public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(8080);


Socket socket = serverSocket.accept();

if (socket.isConnected()) System.out.println("tcp.Server ishga tushdi");

DataInputStream inFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outFromClient = new DataOutputStream(socket.getOutputStream());

String data = inFromClient.readUTF();

String[] a = data.split("~");
int s = 1;

for (int i = 0; i < a.length; i++) {


s *= Integer.parseInt(a[i]);
}

outFromClient.writeUTF(String.valueOf(((float) Math.pow(s, (float) 1/a.length))));


socket.close();
serverSocket.close();
inFromClient.close();
outFromClient.close();
if (socket.isClosed()) System.out.println("tcp.Server yopildi");

}
}



  1. TCP soketi asosida ixtiyoriy R radiusli doira yuzini hisoblash kliyent-server dasturini tuzing.








  1. TCP soketi asosida ixtiyoriy koefitsentli kvadrat tenglama yechimini hisoblash kliyent-server dasturini tuzing.



Download 426.5 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   28




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