Mavzu: udp kliyent-server tarmoq dasturini yaratish


Download 96 Kb.
Sana13.04.2023
Hajmi96 Kb.
#1353057
Bog'liq
Mamadaliyev Kamoliddin 084


Mamadaliyev Kamoliddin 084-20 guruh talabasi
2-LABORATORIYA ISHI
Mavzu: UDP kliyent-server tarmoq dasturini yaratish
Ishning maqsadi: Talabalarda Java dasturlash tili java.net.* va java.io.* paketlari klasslaridan foydalanib UDP kliyent-server tarmoq dasturini yaratish bo‘yicha amaliy ko‘nikmalar hosil qilish.

Bajarish:



ServerSocket kodi:
package tsp;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServersSocket {
public static void main(String argv[]) throws Exception
{
int radius;
String hajm;
double d;
double a = 4/3;
double pi=3.14;
ServerSocket welcomeSocket = new ServerSocket(6789);
System.out.println("Server ishga tushdi!");
System.out.println("Kliyentdan so‘rovni kutmoqda...");
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
System.out.println("Kliyent server bilan bog‘landi");
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new
DataOutputStream(connectionSocket.getOutputStream());
radius = inFromClient.read();
System.out.println("Server kliyentdan so‘rovni qabul qildi");
System.out.println("Kliyentdan qabul qilingan radius: "+radius);
d=radius*radius*radius*a*pi;
hajm=String.valueOf(d);
outToClient.writeBytes(hajm+"\n");
System.out.println("Server qabul qilingan so‘rovni qayta ishlab kliyentga jo‘natdi");
System.out.println("Qayta ishlangan matn: " +hajm);
}
}
}

ClientSocket kodi:


package tsp;
import java.io.*;
import java.net.Socket;
public class ClientSocket {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Kliyent ishga tushdi!!!");
Socket clientSocket = new Socket("localhost", 6789);
System.out.println("Kliyent server bilan bog‘landi");
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Serverga jo‘natsh uchun shar radiusini kiriting:");
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
System.out.println("Kiritilgan radius serverga jo‘natildi");
modifiedSentence = inFromServer.readLine();
System.out.println("Qayta ishlangan matn serverdan keldi: " +
modifiedSentence);
clientSocket.close();
System.out.println("Kliyent soketi yopildi!");
}
}
Dastur natijasi:
Serverni Ishga tushiramiz:

Client ni ishga tushuramiz va shar radiusini kiritamiz:




Download 96 Kb.

Do'stlaringiz bilan baham:




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