DataInputStream给出了java

DataInputStream给出了java

本文介绍了DataInputStream给出了java.io.EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了小型CLI客户端 - 服务器应用程序。加载服务器后,客户端可以连接到服务器并将命令发送到服务器。

I have created small CLI client-server application. Once the server is loaded the client can connect to it and send commands to the server.

第一个命令是获取服务器加载的文件列表。

The first command is to get list of files that server is loaded with.

建立套接字连接后。我请求用户输入命令。

Once the socket connection is established. I request user to enter a command.

ClientApp.java

ClientApp.java

Socket client = new Socket(serverName, serverPort);

Console c = System.console();
if (c == null) {
    System.err.println("No console!");
    System.exit(1);
}

String command = c.readLine("Enter a command: ");

OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF(command);

然后服务器捕获用户的命令并发送相应的回复。

Then the server capture user's command and send appropriate replies.

SeverApp.java -

SeverApp.java -

Socket server = serverSocket.accept();
DataInputStream in = new DataInputStream(server.getInputStream());

switch (in.readUTF()){
    case "list":
        for (String fileName : files) {
            out.writeUTF(fileName);
        }
        out.flush();

}
server.close();

接下来客户端检索服务器的响应 -

Next the client retrieve server's response -

ClientApp.java

ClientApp.java

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
String value;
while((value = in.readUTF()) != null) {
    System.out.println(value);
}

client.close();

files 是一个我保留列表的ArrayList加载到服务器的文件。当客户端向服务器发送 list 命令时,我需要发回字符串数组(文件名列表)。类似的应用程序将有更多的命令。

files is a ArrayList which i keep list of files loaded to the sever. When client sends list command to the sever, i need to send array of strings (list of file names) back. Similarly app will have more commands.

现在,当我做这样的请求时,我得到文件列表和trows java.io.EOFException 来自而((value = in.readUTF())!= null){

Now when i do such request i get list of files and trows java.io.EOFException from while((value = in.readUTF()) != null) {

如何解决这个问题?

编辑(解决方案)---

EDIT (SOLUTION) ---

请注意,DataStreams通过捕获EOFException来检测文件结束条件,而不是测试无效的返回值。 DataInput方法的所有实现都使用EOFException而不是返回值。

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

try {
    while (true) {
        System.out.println(in.readUTF());
    }
    } catch (EOFException e) {
}


推荐答案

请注意,DataStreams通过捕获EOFException来检测文件结束条件,而不是测试无效的返回值。 DataInput方法的所有实现都使用EOFException而不是返回值。

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

try {
    while (true) {
        System.out.println(in.readUTF());
    }
    } catch (EOFException e) {
}

这篇关于DataInputStream给出了java.io.EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:01