异常来袭:java.io.EOFException

jiaojiao_huihui 2011-11-22 06:06:42
今天遇到这样一个异常:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2553)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1296)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at Server.RequestProcessor.run(RequestProcessor.java:40)
at java.lang.Thread.run(Thread.java:662)
网上查了一下说:达了文件的末尾,程序却没有正常结束读取文件内容,
不是太理解什么意思,下面是出错的地方:
public void run() {
boolean flag = true; //是否不间断监听
try{
OnlineClientIOCache currentClientIOCache = new OnlineClientIOCache(
new ObjectInputStream(currentClientSocket.getInputStream()),
new ObjectOutputStream(currentClientSocket.getOutputStream()));
while(flag){ //不停地读取客户端发过来的请求对象
//从请求输入流中读取到客户端提交的请求对象
Request request = (Request)currentClientIOCache.getOis().readObject();
System.out.println("Server读取了客户端的请求:" + request.getAction());

String actionName = request.getAction(); //获取请求中的动作
if(actionName.equals("userRegiste")){ //用户注册
registe(currentClientIOCache, request);
}else if(actionName.equals("userLogin")){ //管理员登录
login(currentClientIOCache, request);
}else if(actionName.equals("userLogin2")){ //用户登录
login2(currentClientIOCache, request);
}else if(actionName.equals("exit")){ //请求断开连接
flag = logout(currentClientIOCache, request);
}else if(actionName.equals("chat")){ //管理员发布信息
chat(request);
}else if(actionName.equals("chat2")){ //客户端聊天
chat2(request);
}else if(actionName.equals("add")){ //客户端聊天
add(request);
}else if(actionName.equals("agree")){ //客户端聊天
add1(request);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
...全文
308 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kouyiSC 2011-11-23
  • 打赏
  • 举报
回复

ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream in = request.getInputStream();
int bytesRead = 0;
while ((bytesRead = in.read()) != -1) {
try {
bos.write(bytesRead);
} catch (Exception e) {
}
}
byte[] bt = bos.toByteArray();
System.out.println(new String(bt, "GBK"));
bos.close();
in.close();
lxbccsu 2011-11-23
  • 打赏
  • 举报
回复
你应该从Socket来监视是否有请求发送过来,当有Request到来时,才读取客户端发过来的请求对象。

一般是writeObject几次,才readObject几次;
所以下面做法肯定会出现EOF异常:
while(flag){ //不停地读取客户端发过来的请求对象
//从请求输入流中读取到客户端提交的请求对象
Request request = (Request)currentClientIOCache.getOis().readObject();

给个测试示例:

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
ByteArrayOutputStream bot = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bot);
oos.writeObject(new Date());
oos.writeObject(3.1415926);
oos.writeObject(2011);
oos.writeObject("Hello World");
oos.close();

ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bot.toByteArray()));
while(true){
try{
Object obj = ois.readObject();
System.out.println(obj);
}catch (Exception e) {
e.printStackTrace();
ois.close();
break;
}
}

}
karl1235 2011-11-22
  • 打赏
  • 举报
回复
这种错误一般是因为对象读取流和对象写入流没对应上引起的..
dracularking 2011-11-22
  • 打赏
  • 举报
回复
看这源码

/**
* Peeks at (but does not consume) and returns the next byte value in
* the stream, or throws EOFException if end of stream/block data has
* been reached.
*/
byte peekByte() throws IOException {
int val = peek();
if (val < 0) {
throw new EOFException();
}
return (byte) val;
}

我觉得你是要对io流是否达到末尾有所判断才能避免这个EOF异常
jiaojiao_huihui 2011-11-22
  • 打赏
  • 举报
回复
我的帖子啊,怎么没人回复呢?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧