调用socket.shutdownOutput()后的EOFException

时间:2013-12-04 20:17:17

标签: 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)

如果您需要任何进一步的信息,请尽快发表评论:)

1 个答案:

答案 0 :(得分:0)

我忘了打电话oos.flush();当服务器还在等待数据时,我关闭了流。这就是EOFException

的原因