gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:21:04 24 4
gpt4 key购买 nike

就像标题所说,在客户端调用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com