客户端服务器在线字典程序在java [英] client-server online dictionary program in java

查看:99
本文介绍了客户端服务器在线字典程序在java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Java中创建一个程序,客户端用英语向服务器发送一个单词,服务器返回其另一种语言的含义(如果字典中存在一个txt文件)。我已经设法连接客户端与服务器,但我有麻烦搜索文件中的单词。
在第一个while循环中,服务器等待来自客户端的输入,当接收到这样的输入时,服务器必须进入第二个while循环,其中,当有未读取的行时,它必须读取它们,并检查从客户端包含在文件的读取行中。如果是 - 服务器将线路发送到客户端。但似乎第二个while循环从来没有访问过。下面是我得到的源代码和输出。请帮我这个。提前感谢。



服务器:

  

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;
文件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(从客户端接受连接);

try {
in = new Scanner(s.getInputStream()); //从客户端输入流
out = new PrintWriter(s.getOutputStream()); //输出流到客户端
inFile = new File(FILE_NAME); //字典文件
readFile = new Scanner(inFile); //扫描文件的扫描程序

String input = null; // String保存从文件中取出的行
while(in.hasNextLine()){
String date = new Date()。toString();
String temp = in.next(); //保存从客户端发送的字的字符串
System.out.println(From the client+ temp);
while(readFile.hasNextLine()){//文件中有未读的行
System.out.println(nextline);
input = readFile.nextLine(); //存储未读的行
System.out.println(从文件+ input);
if(input.contains(temp)){//如果读取的行包含从客户端发送的字
System.out.println(Check+ input ++ temp);
out.println(date ++ input); //用包含其他语言含义的整行进行响应
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(关闭与客户端的连接);

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()); //从服务器输入流
out = new PrintWriter(s.getOutputStream()); //输出流到服务器
}
catch(UnknownHostException ex){
System.err.println(Unknown host:+ PORT);
System.err.println(Exception:+ ex.getMessage());
System.exit(1);
}
catch(IOException ex){
System.err.println(无法获取I / O for+ PORT);
System.err.println(Exception:+ ex.getMessage());
System.exit(1);
}

扫描器用户=新扫描器(System.in); //扫描用户输入
字符串输入;

while(user.hasNext()){
input = user.next(); //保存用户的输入

out.println(input); //发送到服务器
out.flush();

System.out.println(Response:+ in.nextLine());
}

out.close();
in.close();
s.close();
}
}

服务器的输出:



运行:
接受来自客户端的连接
从客户端出口
接收:exit


解决方案

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



如果设置为使用Scanner类处理文件,也许你可以尝试:

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

我通常在读取文本文件时使用BufferedReader。作为 javadocs 说,Scanner非常适合处理正则表达式。 BufferedReader处理逐行读取更好。请查看答案,以便对BufferedReader和Scanner进行一次良好的打击比较。


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

解决方案

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.

这篇关于客户端服务器在线字典程序在java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆