403-guruh talabsi Po’latov Shoxruxning Tarmoqni dasturlash fanidan
2-labaratorya ishi.
Mavzu: Tarmoqda ko‘poqimlilik asosida ishlovchi dasturni yaratish
Ishning maqsadi:
Java dasturlash tili java.net.*, java.io.* va java.util.* paketlari klasslaridan foydalanib tarmoqda ko‘poqimlilik asosida ishlovchi dasturni yaratish.
Topshiriq:
Talaba laboratoriya ishi uchun shaxsiy topshiriq oladi. Bu topshiriq bo‘yicha talaba tarmoqda ko‘poqimlilik asosida ishlovchi dasturni yaratadi. Har bir talaba uchun shaxsiy topshiriq 2.1- jadvalda berilgan.
Nazariy qism:
Thread klassi yordamida oqim yaratish:
class Multi extends Thread{
public void run(){
System.out.println("Oqim ishga tushdi...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
NATIJA: Oqim ishga tushdi...
Dastur kodi:
Klient qismi:
package client;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception
{
String sorov;
String javob1;
String javob2;
Socket clientSocket = new Socket("127.0.0.1", 5522);
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Scanner input = new Scanner(System.in);
System.out.println("\nTortburchak tomonlarini kiriting:");
System.out.print("x = ");
float x = input.nextFloat();
System.out.print("y = ");
float y = input.nextFloat();
if (x>0 && y>0)
{
sorov = x+","+y;
outToServer.writeBytes(sorov += "\n");
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
javob1 = inFromServer.readLine();
javob2 = inFromServer.readLine();
System.out.println("\nTortburchakning yuzasi: " + javob1);
System.out.println("\nTortburchakning perimetri: " + javob2);
clientSocket.close();
}
else
{
System.out.println("\nTortburchak tomonlari noto'g'ri kiritildi");
}
}
}
Server qismi:
package server;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class Server {
static float Yuza(float x, float y)
{
return (x*y);
}
static float Peremetr(float x, float y)
{
return (float)2*(x+y);
}
public static void main(String[] args) throws Exception
{
String clientSorov;
String serverJavob1;
String serverJavob2;
ServerSocket serversocket = new ServerSocket(5522);
while(true)
{
Socket connectionSocket = serversocket.accept();
BufferedReader INPUT = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream OUTPUT = new DataOutputStream(connectionSocket.getOutputStream());
clientSorov = INPUT.readLine();
System.out.println("Quyidagilar qabul qilindi: " + clientSorov);
String[] values = clientSorov.split(",");
ArrayList value = new ArrayList<>();
for (String item: values) {
value.add(Float.parseFloat(item));
}
serverJavob1 = Yuza(value.get(0),+value.get(1))+"";
serverJavob2 = Peremetr(value.get(0),+value.get(1))+"";
OUTPUT.writeBytes(serverJavob1+"\n");
OUTPUT.writeBytes(serverJavob2+"\n");
System.out.println("Yuborildi: " + serverJavob1 +" va " + serverJavob2);
}
}
}
Dasaturdan olingan natija
Do'stlaringiz bilan baham: |