Respuesta :
Here is a sample of how the server would be implemented
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String[] args) {
WordSearcher searcher = new WordSearcher();
try {
// Bind server socket to a port
ServerSocket serverSocket = new ServerSocket(1236);
System.out.println("Server socket created, waiting for clients...");
while (true) {
// Accept client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
// Set up input and output streams
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
// Read in string from client
String inputString = in.readLine();
// Pass string to WordSearcher object and receive list output
List<Integer> positions = searcher.search(inputString);
// Transmit list to client
for (Integer position : positions) {
out.println(position);
}
// Close connection
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The implementation binds a server socket on port 1236 and listens for client connections. When a client connects, it sets up an input stream and an output stream for communicating with the client. It then reads a string from the client, passes it to a Word Searcher object to search for words, and sends a list of resulting positions to the client. Finally the connection is closed and it loops back to wait for the next client connection.
To include a special "shutdown" command, loop checking for that command, then break out of the loop and close the server socket when the command is received. One can also use a flag variable that is set to true when the shutdown command is received, and use that flag to break out of the loop.
Read more about loops on brainly.com/question/29586204
#SPJ4