ObjectInputStream异常 [英] ObjectInputStream exception

查看:104
本文介绍了ObjectInputStream异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
StreamCorruptedException:无效的类型代码:AC

Possible Duplicate:
StreamCorruptedException: invalid type code: AC

大家好,我正在尝试通过网络从流中读取对象...我正在使用ReadInputObject方法...并且正在线程内部实现它以接收数据...这是while循环中的线程

Hello guys I am trying to read object from the stream over the network... I am using ReadInputObject method ... and I am implementing it inside a thread to recieve the data ... here is the while loop in the thread

while((dp = (DataPackage) ois.readObject()) != null)
        {

             Ball b = new Ball();
             b.setX(dp.x);
             b.setY(dp.y);
             jTextField1.setText(b.getX() + "    " +b.getY());
             b.paint(ob.getGraphics());
             b.setVisible(true);
             ois.reset();

        }
    } catch (IOException ex) {
        System.out.println("Error 1 is Here");
    } catch (ClassNotFoundException ex) {
        System.out.println("Error 2 is here");
    }  

问题是ois获取第一个对象,然后.. ...进入异常内部并将其打印出来 错误1在这里

the problem is that the ois gets the first object and then .... it goes inside the exception and prints out this Error 1 is here

这是我发送对象的代码

DataPackage dp = new DataPackage();
    dp.x=b1.getX();
    dp.y=b1.getY();
    dp.t=b1.getT();
    dp.dx=(int)b1.getDx();
    dp.dy=(int)b1.getDy();

    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(Server_login.client.getOutputStream());
    } catch (IOException ex) {
        Logger.getLogger(Game_Painters_right.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        oos.writeObject(dp);
    } catch (IOException ex) {
        Logger.getLogger(Game_Painters_right.class.getName()).log(Level.SEVERE, null, ex);
    }

我的代码有什么问题? 谢谢

what is the wrong in my code ?? thank you

推荐答案

好,删除对ois.reset()的调用.为什么首先出现在这里?

Well, remove the call to ois.reset(). Why is it there in the first place?

这篇关于ObjectInputStream异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆