【问题标题】:DataInputStream giving java.io.EOFExceptionDataInputStream 给出 java.io.EOFException
【发布时间】:2014-04-14 16:08:01
【问题描述】:

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

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

一旦建立了套接字连接。我要求用户输入命令。

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);  

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

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();

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

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 命令时,我需要发送回字符串数组(文件名列表)。同样app会有更多的命令。

现在,当我提出此类请求时,我会从 while((value = in.readUTF()) != null) { 获取文件列表和 java.io.EOFException

如何解决这个问题?


编辑(解决方案)---

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

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

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

【问题讨论】:

标签: java serversocket eofexception


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    使用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);
    }
    
    ...
    

    【讨论】:

    • Braj 我有另一个问题。在我的代码中,我遍历数组并将数据一一发送给客户端。是否可以将其作为 string[] 发送一次?
    • 抱歉,该解决方案似乎不起作用。它给java.net.SocketException: Software caused connection abort: socket write error 来自out.writeUTF(fileName);
    • 它对我来说是工作文件。请看我更新的帖子。
    • 找出区别并告诉我你做错了什么?
    • 问题出在服务器端代码中,您在while 循环中关闭了服务器套接字serverSocket.close(); 并尝试在同一个服务器套接字上接受另一个客户端套接字。为了解决这个问题,将serverSocket.close(); 移到while 循环之外。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-11-09
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2018-06-14
      • 1970-01-01
      • 2015-03-10
      • 2018-06-18
      相关资源
      最近更新 更多