DataInputStream 给出 java.io.EOFException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23064917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:24:16  来源:igfitidea点击:

DataInputStream giving java.io.EOFException

javaserversocketeofexception

提问by ChamingaD

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.

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

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

客户端应用程序

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

客户端应用程序

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

client.close();

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

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

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

现在,当我做了这样的要求,我得到的文件和trows列表java.io.EOFExceptionwhile((value = in.readUTF()) != null) {

How to fix this ?

如何解决这个问题?



EDIT (SOLUTION) ---

编辑(解决方案)---

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

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.

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

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

采纳答案by ChamingaD

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

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.

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

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

回答by Braj

Use FilterInputStream.available().

使用FilterInputStream.available()

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

...

回答by JamesB

The method readUTF will never return null. Instead, you should do:

readUTF 方法永远不会返回 null。相反,你应该这样做:

while(in.available()>0) {
    String value = in.readUTF();

Looking at javadocs, an EOFException is thrown if this input stream reaches the end before reading all the bytes.

查看 javadocs,如果此输入流在读取所有字节之前到达末尾,则会引发 EOFException。