0% found this document useful (0 votes)
4 views4 pages

Dhus

The document contains a Java implementation of a string processing server and client. The server listens for client connections and processes string commands such as converting to uppercase, lowercase, reversing, removing duplicates, counting characters, and finding the most frequent character. The client connects to the server, sends a command and data, and receives the processed result from the server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Dhus

The document contains a Java implementation of a string processing server and client. The server listens for client connections and processes string commands such as converting to uppercase, lowercase, reversing, removing duplicates, counting characters, and finding the most frequent character. The client connects to the server, sends a command and data, and receives the processed result from the server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.io.

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

public class StringProcessingServer {


public static void main(String[] args) {
int port = 12345; // Cổng server
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server đang lắng nghe trên cổng " + port);

while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client đã kết nối!");

BufferedReader input = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);

String command = input.readLine();


String data = input.readLine();

String result = handleCommand(command, data);


output.println(result);

socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String handleCommand(String command, String data) {
switch (command) {
case "UPPERCASE":
return data.toUpperCase();
case "LOWERCASE":
return data.toLowerCase();
case "REVERSE":
return new StringBuilder(data).reverse().toString();
case "REMOVE_DUPLICATES":
return removeDuplicates(data);
case "CHAR_COUNT":
return countCharacters(data);
case "MOST_FREQUENT":
return mostFrequentCharacter(data);
default:
return "Lệnh không hợp lệ!";
}
}

private static String removeDuplicates(String data) {


StringBuilder result = new StringBuilder();
Set<Character> seen = new HashSet<>();
for (char c : data.toCharArray()) {
if (!seen.contains(c)) {
seen.add(c);
result.append(c);
}
}
return result.toString();
}

private static String countCharacters(String data) {


Map<Character, Integer> charCount = new HashMap<>();
for (char c : data.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
return charCount.toString();
}

private static String mostFrequentCharacter(String data) {


Map<Character, Integer> charCount = new HashMap<>();
for (char c : data.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
return charCount.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(e -> "Ký tự: " + e.getKey() + ", Xuất hiện: " + e.getValue() + " lần")
.orElse("Không tìm thấy ký tự!");
}
}

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

public class StringProcessingClient {


public static void main(String[] args) {
String serverAddress = "127.0.0.1"; // Địa chỉ server
int port = 12345;

try (Socket socket = new Socket(serverAddress, port)) {


BufferedReader console = new BufferedReader(new
InputStreamReader(System.in));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

System.out.println("Nhập lệnh (UPPERCASE, LOWERCASE, REVERSE,


REMOVE_DUPLICATES, CHAR_COUNT, MOST_FREQUENT):");
String command = console.readLine();

System.out.println("Nhập chuỗi:");
String data = console.readLine();

output.println(command);
output.println(data);

String result = input.readLine();


System.out.println("Kết quả từ server: " + result);

} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like