O’ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
“Tarmoqni dasturlash asoslari” fanidan
1-laboratoriya ishi
Mavzu: “TCP kliyent-server tarmoq dasturini yaratish”
Bajardi: Sulaymonov Ahror
Guruh: 044-19
Toshkent – 2023
20-variant
Ixtiyoriy matnning juft o‘rindagi haflarini ajratib yozish
Client.java faylini yaratamiz va unga quyidagicha kod yozamiz.
Endi Server.java faylini yaratib quyidagi kodni yozamiz.
Server.java faylini ishga tushiramiz va quyidagicha natijaga ega bo’lamiz:
Endi Client.java faylini ishga tushiramiz.
Matn kiritamiz:
Client.java
package sulaymonov_ahror;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 9999);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter text: ");
String text = userInput.readLine();
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
output.println(text);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String separatedText = input.readLine();
System.out.println("Separated text: " + separatedText);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server.java:
package sulaymonov_ahror;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("Server started. Waiting for clients...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
// Read text from the client
String text = input.readLine();
// Separate letters at even places
StringBuilder separatedText = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
if (i % 2 == 1) {
separatedText.append(text.charAt(i)).append(" ");
} else {
separatedText.append(text.charAt(i));
}
}
// Send separated text back to the client
output.println(separatedText.toString());
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Dostları ilə paylaş: |