java中的客户端 - 服务器在线词典程序

时间:2014-05-20 12:21:02

标签: java file dictionary client-server

我正在尝试用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

1 个答案:

答案 0 :(得分:0)

这可能是dictionary.txt的编码问题。当EOL标记不同时,可能在操作系统之间移动了意外的字符集或文件。

如果您已开始使用Scanner类处理文件,也许您可​​以尝试:

new Scanner(new BufferedReader(new FileReader(file)));

在读取文本文件时,我通常使用BufferedReader。正如javadocs所说,Scanner非常适合处理正则表达式。 BufferedReader可以更好地逐行处理。查看this答案,了解BufferedReader和Scanner的详细比较。

相关问题