I have a bug in my Java code. Could you help me with it?

Discussion in 'Programming' started by Bolt, May 4, 2015.

  1. I pried the web for a solution but can't seem to find any. I'm creating a Chat Room application in Java and I have no idea why my program throws a StreamCorruptedException when I initialize my ObjectInputStream in my server listener.

    Server listener :
    Code (Text):
    private void startServerListener() {

            while (true) {

                try {

                    ObjectOutputStream writer;
                    ObjectInputStream reader = new ObjectInputStream(
                            new BufferedInputStream(socket.getInputStream())); // Error here.

                    Message msg = (Message) reader.readObject();
                    System.out.println(3);
                    if (msg.hasValue("Username")) {
                        System.out.println(4);
                        this.username = msg.getValue("Username");
                        msg.remove("Username");
                        Message tmp_msg = new Message();
                        tmp_msg.addValue("Raw Message", username + " has joined");

                        for (Listener l : Server.clients) {

                            Socket socket = l.getSocket();
                            writer = new ObjectOutputStream(
                                    new BufferedOutputStream(
                                            socket.getOutputStream()));
                            writer.writeObject(tmp_msg);
                            writer.flush();

                        }

                    }
                    if (msg.hasValue("Message")) {
                        System.out.println(5);
                        for (Listener l : Server.clients) {

                            Socket socket = l.getSocket();
                            writer = new ObjectOutputStream(
                                    new BufferedOutputStream(
                                            socket.getOutputStream()));
                            writer.writeObject(msg.getValue("Message"));
                            writer.flush();

                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
     
    Stack trace :
    Code (Text):
    java.io.StreamCorruptedException: invalid stream header: 7371007E
        at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
        at java.io.ObjectInputStream.<init>(Unknown Source)
        at com.Listener.startServerListener(Listener.java:89)
        at com.Listener.access$1(Listener.java:82)
        at com.Listener$2.run(Listener.java:37)
        at java.lang.Thread.run(Unknown Source)
     
     
  2. hastebin.com and im not java dev but i think its missing a stream header lawel