java - 调用 socket.shutdownOutput() 后出现 EOFException

标签 java android client-server eofexception

就像标题所说,在客户端调用 shutdownOutput() 后,我在服务器端收到 EOFException

这是在服务器端:

    public void getRestaurant() {     
String tempRestaurant=null;
try { BufferedReader fr =
           new BufferedReader( new FileReader( "Restaurant.txt" ));
      tempRestaurant = fr.readLine();
      System.out.println( tempRestaurant );
      System.out.println("writing tempRestaurant is the next Step");
    oos.writeObject(tempRestaurant);
    System.out.println("tempRestaurant has been written");
    oos.close();
    fr.close();
} catch (IOException ex) {
    ex.printStackTrace();
 }        
}

这是客户端的代码:

    protected String doInBackground(Void... params) {
    connecttoServer();
    System.out.println("connecting to server...");
    try {
        oos.writeInt(1);
        System.out.println("next step is closing");
        serverside.shutdownOutput();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        System.out.println("connected to server");
        Restaurant=(String) ois.readObject();
        System.out.println("doInBackground(): "+Restaurant);    

这是错误代码:

    java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(Unknown Source)
at java.io.ObjectInputStream.readInt(Unknown Source)
at prealphaserverpackage.clientsidethreads.handlerequest(Serverpart.java:355)
at prealphaserverpackage.clientsidethreads.run(Serverpart.java:156)

如果您需要任何进一步的信息,请发表评论,我会尽快将它们放在网上:)

最佳答案

我忘记调用oos.flush();当服务器仍在等待数据时,我关闭了流。这就是 EOFException 的原因。

关于java - 调用 socket.shutdownOutput() 后出现 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385028/

相关文章:

sockets - 套接字创建-删除过程是否非常昂贵?

java - 了解 JDBC 内部结构

java - 如何获取整行或整列对象的状态是一个二维数组

android - ARCore 不显示对象的真实尺寸

android - 检测 TextView 是否跨越 2 行

c - 如何将数据从数组发送到指针变量并从指针变量返回到数组?

java - 增加 Excel 2010 内存分配

java - 用Java实现单例数据库连接

java - Android getAltitude 基于大气压和传感器压力

c - 如果我想从客户端向服务器一一写入多个请求,该怎么办?