Лабораторная работа №1 на тему: tcp socket Утбосаров А. Студент группы 421-18 Проверила: Ахмедова Х. Ташкент 2021


Download 0.89 Mb.
Sana16.02.2023
Hajmi0.89 Mb.
#1203128
TuriЛабораторная работа
Bog'liq
Lab 1


МИНИСТЕРСВО ПО РАЗВИТИЮ ИНФОРМАЦИОННЫХ ТЕХНОЛОГИЙ И КОММУНИКАЦИЙ РЕСПУБЛИКИ УЗБЕКИСТАН
ТАШКЕНТСКИЙ УНИВЕРСИТЕТ ИНФОРМАЦИОННЫХ ТЕХНОЛОГИЙ ИМЕНИ МУХАММАД АЛ-ХОРАЗИМИЙ


Лабораторная работа № 1

НА ТЕМУ: TCP Socket


Выполнил: Утбосаров А.
Студент группы 421-18
Проверила: Ахмедова Х.

Ташкент – 2021



Тема: Создание сетевого приложения TCP клиент- сервер.
Создадим пакет «tcp» и два класса в нём: «TCPClient» и «TCPServer»




//Client package tcp; import java.io.*; import java.net.*; import java.util.Scanner; class Client { public static void main(String argv[]) throws Exception { String modifiedSentence; System.out.println("The client started!!!"); Socket clientSocket = new Socket("localhost", 8888); System.out.println("The client is connected to the server"); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); System.out.println("Enter an optional number to send to the server:"); Scanner scanner = new Scanner(System.in); String mySentence = scanner.nextLine(); String mySentence2 = scanner.nextLine(); String nums = mySentence + "," + mySentence2; outToServer.writeBytes(nums + '\n'); System.out.println("The entered number was sent to the server"); modifiedSentence = inFromServer.readLine(); System.out.println("The processed number came from the server: " + modifiedSentence); clientSocket.close(); System.out.println("The client socket is closed!"); } } ########################################################### #Server package tcp; import java.io.*; import java.net.*; class Server { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(8888); System.out.println("The server started!"); System.out.println("Waiting for a request from the client..."); while (true) { Socket connectionSocket = welcomeSocket.accept(); System.out.println("The client is connected to the server"); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); System.out.println("The server received a request from the client"); System.out.println("The number received from the client: " + clientSentence); String[] sentenceFromClient = clientSentence.split(","); double[] d = new double[2]; double square = 0; for (int i = 0; i < sentenceFromClient.length; i++) { d[i] = Double.parseDouble(sentenceFromClient[i]); } square = d[0] * d[1]; capitalizedSentence = Double.toString(square) + '\n'; outToClient.writeBytes(capitalizedSentence); System.out.println("The server processed the received request and sent it to the client"); System.out.println("Processed straight rectangular face: " + capitalizedSentence); } } } //Screenshots


Download 0.89 Mb.

Do'stlaringiz bilan baham:




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