我正在尝试用Java创建一个程序,其中客户端用英语向服务器发送一个单词,服务器以另一种语言返回其含义(如果单词存在于字典中,这是一个txt文件))。我已设法将客户端与服务器连接,但我在搜索文件中的单词时遇到了麻烦。 在第一个while循环中,服务器等待来自客户端的输入,当收到这样的输入时,服务器必须进入第二个while循环,而当有未读行时,它必须读取它们并检查从客户端发送的单词是否包含在来自文件的读取行。如果是 - 服务器将该行发送到客户端。但似乎永远不会访问第二个while循环。以下是我得到的源代码和输出。请帮帮我。提前谢谢。
服务器:
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();
}
}
}
客户:
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();
}
}
服务器的输出:
run:
Accepted connection from client
From the client exit
Received: exit