问题
I am trying to create a program in Java where a client sends a word to the server in English and the server returns its meaning in another language (if the word exists in the dictionary which is a txt file)). I have managed to connect the client with the server but I have troubles with searching the word in the file. In the first while loop the server waits for input from the client, when such is received the server has to enter the second while loop where while there are unread lines it has to read them and check if the word sent from the client is contained in the read line from the file. If it is - the server sends the line to the client. But it seems that the second while loop is never accessed. Below are the source codes and the output which I get. Please help me on this. Thanks in advance.
The server:
package dictionaryserver;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.Scanner;
public class DictionaryServer {
public static void main(String[] args) throws IOException {
final int PORT = 8888;
final String FILE_NAME = "dictionary.txt";
ServerSocket server = new ServerSocket(PORT);
Socket s = null;
Scanner in = null;
PrintWriter out = null;
File inFile = null;
Scanner readFile = null;
while (true) {
try {
s = server.accept();
}
catch (IOException ex) {
System.err.println("Accept failed");
System.err.println("Exception: " + ex.getMessage());
System.exit(1);
}
System.out.println("Accepted connection from client");
try {
in = new Scanner(s.getInputStream()); // Input stream from the client
out = new PrintWriter(s.getOutputStream()); // Output stream to the client
inFile = new File(FILE_NAME); // The dictionary file
readFile = new Scanner(inFile); // Scanner which scans the file
String input = null; // String holding the line taken from the file
while (in.hasNextLine()) {
String date = new Date().toString();
String temp = in.next(); // String holding the word sent from the client
System.out.println("From the client " + temp);
while (readFile.hasNextLine()) { // While there are unread lines in the file
System.out.println("nextline");
input = readFile.nextLine(); // Store the unread line
System.out.println("From the file " + input);
if (input.contains(temp)) { // If the read line contains the word sent from the client
System.out.println("Check " + input + " " + temp);
out.println(date + " " + input); // Respond with the whole line containing the meaning in the other language
out.flush();
}
else {
out.println("No knowledge for " + temp);
out.flush();
}
}
System.out.println("Received: " + temp);
}
}
catch (IOException ex) {
System.err.println("Exception: " + ex.getMessage());
System.out.println("Closing connection with client");
out.close();
in.close();
System.exit(1);
}
out.close();
in.close();
}
}
}
The client:
package dictionaryclient;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class DictionaryClient {
public static void main(String[] args) throws IOException {
final int PORT = 8888;
Socket s = null;
Scanner in = null;
PrintWriter out = null;
try {
s = new Socket("localhost", PORT);
in = new Scanner(s.getInputStream()); // Input stream from the server
out = new PrintWriter(s.getOutputStream()); // Output stream to the server
}
catch (UnknownHostException ex) {
System.err.println("Unknown host: " + PORT);
System.err.println("Exception: " + ex.getMessage());
System.exit(1);
}
catch (IOException ex) {
System.err.println("Cannot get I/O for " + PORT);
System.err.println("Exception: " + ex.getMessage());
System.exit(1);
}
Scanner user = new Scanner(System.in); // Scanning for user input
String input;
while (user.hasNext()) {
input = user.next(); // Hold the input from the user
out.println(input); // Send it to the server
out.flush();
System.out.println("Response: " + in.nextLine());
}
out.close();
in.close();
s.close();
}
}
The output from the server:
run:
Accepted connection from client
From the client exit
Received: exit
回答1:
It is likely an encoding issue with dictionary.txt. Either an unexpected character set or the file may have been moved between operating systems when EOL markings are different.
If you are set on using the Scanner class to process the file, perhaps you can try:
new Scanner(new BufferedReader(new FileReader(file)));
I generally use BufferedReader when reading in files that are text files. As the javadocs say, Scanner is great for dealing with regex. BufferedReader handles line by line reading much better. Check out this answer for a good blow-by-blow comparison of BufferedReader and Scanner.
来源:https://stackoverflow.com/questions/23759687/client-server-online-dictionary-program-in-java