java中的迭代字典服务器
它的作用。。 1.正在侦听客户端请求的迭代字典服务器。。 2.将建立连接。。 3.服务器将接受来自客户端的输入字符串。。 4.然后服务器将从文件中搜索字符串的含义。。 5.然后服务器将向客户端返回含义 问题在于服务器的while循环。。如果它找到了这个词,它会把这个词的意思发送给客户。。但是如果找不到这个词。。。这个java中的迭代字典服务器,java,file,dictionary,stream,client-server,Java,File,Dictionary,Stream,Client Server,它的作用。。 1.正在侦听客户端请求的迭代字典服务器。。 2.将建立连接。。 3.服务器将接受来自客户端的输入字符串。。 4.然后服务器将从文件中搜索字符串的含义。。 5.然后服务器将向客户端返回含义 问题在于服务器的while循环。。如果它找到了这个词,它会把这个词的意思发送给客户。。但是如果找不到这个词。。。这个 if(d.equals(null)){ input="No knowledge"; out.println(input); out.flush(); }
if(d.equals(null)){
input="No knowledge";
out.println(input);
out.flush();
}
不执行。。。客户端说空,服务器说空异常。。。
我在这里做错了什么。。。我不明白
我已尝试更改此代码。。。
客户:
导入java.io。;
导入java.net。;
导入java.net.Socket;
导入java.net.UnknownHostException;
导入java.util.Scanner
public class DCC1 {
public static void main(String[] args) throws IOException {
final int PORT = 8888;
Socket s = null;
PrintWriter out = null;
try {
s = new Socket("localhost", PORT);
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
System.out.print("Enter String: ");
String input;
input = user.next(); // Hold the input from the user
out.println(input); // Send it to the server
out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream( )));
System.out.println(br.readLine());
out.close();
s.close();
}
}
服务器:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class DSC1
{
public static void main(String[] args) throws IOException
{
final int PORT = 8888;
final String FILE_NAME = "dictionary.dat";
ServerSocket server = new ServerSocket(PORT);
Socket s = null;
PrintWriter out = null;
Scanner in = null;
FileInputStream fin = null;
ObjectInputStream oin = 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
String temp = in.next(); // String holding the word sent from the client
System.out.println("From the client " + temp);
String input = null;
fin = new FileInputStream(FILE_NAME);// The dictionary file
oin = new ObjectInputStream(fin);
dictionary d = (dictionary)oin.readObject();
while(d!= null)
{
System.out.println("in loop...");
if(d.name.equals(temp)){
input=d.meaning;
d.printDic();
out.println(input);
out.flush();
break;
}
d = (dictionary)oin.readObject();
if(d.equals(null))
{
input="No knowledge";
out.println(input);
out.flush();
}
}
}
catch (ClassNotFoundException|IOException ex)
{
System.err.println("Exception: " + ex.getMessage());
System.out.println("Closing connection with client");
in.close();
System.exit(1);
}
in.close();
}
}
}不要使用d.equalNull。如果要检查d是否为null,只需检查d==null
为什么??这无法返回true,因此可能这就是为什么此代码从未执行,并且您没有得到预期结果的原因。这确实是更好的代码,但它没有回答问题。@jbnize不太好,OP希望此if条件有时会计算为true。谢谢@JBNizet peter.petrov..不知道如何告诉用户未找到输入的单词。。!!如果您遇到异常并想知道原因,那么发布此异常的完整堆栈跟踪。请注意,当您到达ObjectInputStream的末尾并尝试读取对象时,不会得到null,而是一个异常。这是有案可查的。