Private static ServerSocket


Download 40.26 Kb.
Sana18.06.2023
Hajmi40.26 Kb.
#1582069
Bog'liq
11-lab ish


O‘ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
11-Laboratoriya ish

Bajardi:411-19-guruh talabasi


Xabibullayev Qodirjon
Tekshirdi: Abdullayev Ulug’bek

Toshkent 2022


28-Variant
Savol:Koeffitsentli kvadrat tenglama yechimini topish
server.java
package laboratoriya_11;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private static ServerSocket serverSocket;
private static final int PORT = 1234;
public static void main(String[] args)
throws IOException {
try {
serverSocket = new ServerSocket(PORT);

System.out.println("\nServer soket ishga tushdi...\nKliyentni kutmoqda...");


} catch (IOException ioEx) {
System.out.println("\nPortni o‘rnata olmadim!");
System.exit(1);
}
do {
Socket client = serverSocket.accept();
System.out.println("Yangi kliyent qabul qilindi.");
ClientHandler handler = new ClientHandler(client);
handler.start();
} while (true);
}
}
class ClientHandler extends Thread {
private Socket client;
private Scanner input;
private PrintWriter output;
public ClientHandler(Socket socket) {
client = socket;
try {
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
}
public void run() {
String as, bs, cs;
float S;
double D, x1, x2, x;
do {

as = input.nextLine();


bs = input.nextLine();
cs = input.nextLine();
int a = Integer.parseInt(as);
int b = Integer.parseInt(bs);
int c = Integer.parseInt(cs);
D = (double) (Math.pow(b, 2) - 4 * a * c);
if (D > 0) {
x1 = (double) (-b + Math.sqrt(D)) / (2 * a);
x2 = (double) (-b - Math.sqrt(D)) / (2 * a);
System.out.println("Tenglamaning birinchi ildizi: " + x1);
System.out.println("Tenglamaning ikkinchi ildizi: " + x2);
} else if (D == 0) {
x = -b / (2 * a);
System.out.println("Tenglamaning ildizi: " + x);
} else {
System.out.println("Tenglama yechimga ega emas !!!");
}
//Repeat above until 'QUIT' sent by client…
} while (!(as.equals("Q") bs.equals("Q") cs.equals("Q")));
try {
if (client != null) {
System.out.println("Bog‘lanish uzildi");
client.close();
}
} catch (IOException ioEx) {
System.out.println("Bog‘lanish uzilmadi!");
}
}
}

Client.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package laboratoriya_11;

import java.io.*;
import java.net.*;
import java.util.*;

public class Client {

private static InetAddress host;
private static final int PORT = 1234;

public static void main(String[] args) {
try {
host = InetAddress.getLocalHost();
} catch (UnknownHostException uhEx) {
System.out.println("\nID host topilmadi!\n");
System.exit(1);
}
sendMessages();
}
private static void sendMessages() {
Socket socket = null;
try {
socket = new Socket(host, PORT);
Scanner networkInput = new Scanner(socket.getInputStream());
PrintWriter networkOutput = new PrintWriter(socket.getOutputStream(),
true);
Scanner userEntry = new Scanner(System.in);
String message1, message2, message3, response;
do {
System.out.print("\na: (Chiqish uchun 'Q'): ");
message1 = userEntry.nextLine();
networkOutput.println(message1);
System.out.print("\nb: (Chiqish uchun 'Q'): ");
message2 = userEntry.nextLine();
networkOutput.println(message2);
System.out.print("\nc: (Chiqish uchun 'Q'): ");
message3 = userEntry.nextLine();
networkOutput.println(message3);
response = networkInput.nextLine();
System.out.println("SERVER> " + response);
} while ((!message1.equals("Q") !message3.equals("Q") !message3.equals("Q") ));
} catch (IOException ioEx) {
ioEx.printStackTrace();
} finally {
try {
System.out.println("\nBog‘lanish uzildi…");
socket.close();
} catch (IOException ioEx) {
System.out.println("Bog‘lanish uzilmadi!");
System.exit(1);
}
}
}
}



Download 40.26 Kb.

Do'stlaringiz bilan baham:




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