Java socket related classes to create a dictionary client To get familiar with the socket library in Java,
· Requesting, receiving, parsing and returning a list of matching strategies supported by the server. The protocol allows a client to retrieve a list of matches (suggestions) based on a keyword, and the strategy is used to indicate how these keywords are used to present actual dictionary entries. Examples include prefix matches (all words that start with a prefix), regular expressions, entries with similar sounding words, etc. In the interface, the user will have the option of selecting a specific strategy for matching, with prefix match being the default.
· Requesting, receiving, parsing and returning a list of matches based on a keyword, a matching strategy and a database. This list of matches will be used in the interface to suggest entries as they type a word.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static String[] dictionaries = {"English", "Thesaurus", "English-French", "Dictionary of acronyms"};
public static void handler(Socket socket) {
Thread thread = new Thread(() -> {
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String line;
out.println("HELLO");
while ((line = in.readLine()) != null) {
if (line.equals("LIST")) {
StringBuilder builder = new StringBuilder();
for (String dictionary : dictionaries) {
builder.append(dictionary).append(",");
}
out.println("LIST > " + builder);
} else if (line.equals("QUIT")) {
out.println("BYE");
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
thread.setDaemon(true);
thread.start();
}
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(2628)) {
while (true) {
handler(serverSocket.accept());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void writer(PrintWriter out) {
Thread thread = new Thread(() -> {
Scanner in = new Scanner(System.in);
while (true) {
out.println(in.nextLine());
}
});
thread.setDaemon(true);
thread.start();
}
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 2628)) {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
writer(out);
String line;
while ((line = in.readLine()) != null) {
if (line.equals("HELLO")) {
System.out.println(line);
} else if (line.startsWith("LIST > ")) {
String[] dictionaries = line.split(" > ")[1].split(",");
System.out.println("Dictionaries:");
for (int i = 1; i < dictionaries.length; i++) {
System.out.println(dictionaries[i]);
}
} else if (line.equals("BYE")) {
System.out.println(line);
socket.close();
}
}
} catch (Exception e) {
}
}
}
Comments
Leave a comment