【问题标题】:ObjectInputStream doesn´t get initializedObjectInputStream 没有被初始化
【发布时间】:2015-12-28 19:03:49
【问题描述】:

我的 ObjectInputStream 有问题。我想通过这个发送对象,但是程序在我想初始化 ObjectInputStream 的地方停止。我已经搜索过已回答的问题,发现需要打开 ObjectOutputstream,然后才能初始化 ObjectInputStream。但这肯定是在那里完成的。

我的客户端类的一段代码:

socket = new Socket(InetAddress.getByName(server).getHostAddress(), 13340);
        messages = new Scanner(socket.getInputStream());
        p = new PrintStream(socket.getOutputStream());
        ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream());

        p.println("addServerContent");

        o.flush();
        System.out.println("1");
        o.writeObject(String.valueOf(index));
        o.writeObject(s);

这是服务器的部分:

}else if(what.equals("addServerContent")){
        ObjectInputStream i = null;
        try{
            i = new ObjectInputStream(s.getInputStream());
        }catch(IOException e){}

        while(GxMS2.ListUsed){
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {}
        }

        try{

            System.out.println("1");
            int index = Integer.parseInt((String)i.readObject());
            ServerContent sc = (ServerContent)i.readObject();

在服务器上它甚至没有达到“1”标记。

为什么它不起作用?

谢谢

【问题讨论】:

  • 它要么挂在这里while(GxMS2.ListUsed),要么挂在其他地方......
  • 不,它不存在,我在while前面加了一个标记,它没有被触发
  • "我发现 ObjectOutputstream 需要打开才能初始化 ObjectInputStream" 绝对不正确
  • stackoverflow.com/questions/14551211/… 答案 2,这是我发现的,看起来很合乎逻辑

标签: java stream serversocket objectinputstream


【解决方案1】:

您不能将字符流与对象流结合起来,因此您可以将 OutputStreamPrintWriterObjectOutputStream 一起包装,但不能同时使用两者。

【讨论】:

  • 为什么不呢?他们是不是互相屏蔽了?
猜你喜欢
  • 2018-05-03
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
相关资源
最近更新 更多