Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: StreamCorruptedException invalid type code: 00

  1. #1
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default StreamCorruptedException invalid type code: 00

    I am making a (client/server architecture) LAN chat application. I am using ObjectInputStream and ObjectoutputStream to send and receive the message between client and server. The server side works well. In client side there are two threads, each reads the message from same objectinputstream. But I get the exception mentioned in the title above. So my question is whether two threads can read from same stream or not?

    here's my code

    the Message class is used for identifying whether the message is intended for all users (i.e group chat) or just a specific user (i.e private chat)

    Java Code:
    public class Message implements Serializable
    {
        public String sender;
        public String receiver;
        public String user;
        public String content;
    }

    the following thread is the one that reads the message sent from server and prints it to the client's group chat screen. also if the message is private it initialize another class for the private messaging.

    Java Code:
    class RecvThread extends Thread
        {
            @Override
            public void run()
            {
                try
                {
                    chat_msg=new Message();
                    while(isConnected=true)
                    {
                       
                        chat_msg=(Message) oips.readObject();
                        if((chat_msg.user).equals("new_user_login"))
                        {
                            if((chat_msg.receiver).equals(username))
                            {
                                showMsgTxtArea.append("Welcome to the chatroom...." +"\n");
                                showMsgTxtArea.setCaretPosition(showMsgTxtArea.getText().length());
                            }
                            else if(!(chat_msg.receiver).equals(username))
                            {
                                onuser.add(chat_msg.receiver);
                                Collections.sort(onuser);
                                onlineUsersTxtArea.setText(null);
                                for(String s:onuser)
                                {
                                    onlineUsersTxtArea.append(s+"\n");
                                }
                                showMsgTxtArea.append(chat_msg.receiver +" has entered the chatroom"+"\n");
                                showMsgTxtArea.setCaretPosition(showMsgTxtArea.getText().length());
                            }
                        }
                        else if((chat_msg.user).equals("user_logout") & !(chat_msg.receiver).equals(username))
                        {
                            onuser.remove(chat_msg.receiver);
                            Collections.sort(onuser);
                            onlineUsersTxtArea.setText(null);
                            for(String s:onuser)
                            {
                                onlineUsersTxtArea.append(s+"\n");
                            }
                            showMsgTxtArea.append(chat_msg.receiver+ " has left the chatroom"+"\n");
                            showMsgTxtArea.setCaretPosition(showMsgTxtArea.getText().length());
                        }
                        else if((chat_msg.user).equals("online"))
                        {
                            showMsgTxtArea.append(chat_msg.sender + ": "+chat_msg.content+ "\n");
                            showMsgTxtArea.setCaretPosition(showMsgTxtArea.getText().length());
                        }
                        else if((chat_msg.user).equals("private"))
                        {
                            if((pvt_user.indexOf(chat_msg.sender))==-1)
                            {
                                PrivateChat1 chat;
                                chat = new PrivateChat1(chat_msg.sender,chat_msg.content);
                                chat.setVisible(true);
                            }
                        }
                    }
                }
                catch(Exception e)
                {
                    try {
                        JOptionPane.showMessageDialog(null, "Disconnected");
                        isConnected=false;
                        usernameTxtField.setEditable(true);
                        connectButton.setText("Connect");
                        onuser.clear();
                        onlineUsersTxtArea.setText(null);
                        oops.close();
                        oips.close();
                        sock.close();
                    } catch (IOException ex) {
                        Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    the following code is for the private message. the thread reads the message sent from the server and if its private then prints it in the specific screen.

    Java Code:
    public class PrivateChat1 extends javax.swing.JFrame {
       
            private final String prv_usr_name;
            public Message p_chat_msg;
            P_chat p;
            StyledDocument doc;
            public PrivateChat1(String u_name) {
                
                this.prv_usr_name=u_name;
                p=new P_chat(u_name);
                initComponents();
                doc=prv_msg_screen.getStyledDocument();
                this.p_chat_msg=new Message();
                setTitle(prv_usr_name);
                pvt_user.add(prv_usr_name);
        }
           public PrivateChat1(String u_name, String mesg)
            {
              this(u_name);
                try {
                    this.doc.insertString(doc.getLength(), u_name + ": "+ mesg +"\n", null);
                } catch (BadLocationException ex) {
                    Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    class P_chat extends Thread
        {
            private final String p_user;
            P_chat(String p_name)
            {
                p_user=p_name;
            }
           @Override
           public void run()
           {
               System.out.println("Private chat "+ this.p_user);
                try
                {
                    while(isConnected=true)
                    {
                       p_chat_msg=(Message) oips.readObject();
                        if((p_chat_msg.user).equals("private"))
                        {
                            System.out.println("Private chat "+ this.p_user);
                            if((p_chat_msg.sender).equals(this.p_user))
                            {
                                doc.insertString(doc.getLength(), p_user + ": "+ p_chat_msg.content +"\n", null);
                            }
                        }
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
               
           }
        }
    the stacktrace shows that the exception is caused because of line 41 in P_chat Thread. is this because the two threads are reading from same stream at the same time?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,993
    Rep Power
    5

    Default Re: StreamCorruptedException invalid type code: 00

    Very likely, don't do that. Why would you even want to do that? Why did you think that it is possible to do that? You know and understand how streams work, don't you?

    If you must have several threads operating on the same data, which I already kind of doubt, you had better make one thread responsible for reading it and adding it to some sort of object storage which multiple threads can access. Which will require synchronization to make it stable.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Re: StreamCorruptedException invalid type code: 00

    can you give me an example of creating an object storage and using it with synchronization? In this case the object would be Message.

    P.S I am not asking you without trying. I am kinda new to threads and synchronization, so my effort wasn't successful.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: StreamCorruptedException invalid type code: 00

    Have one thread that owns the object input stream.
    It's sole purpose is to read from the stream creating objects.
    It adds these to a queue of incoming messages.
    Other threads read from this queue, via a synchronised method.

    Or have the reading thread prompt a listener when a new object is complete.
    It's the listeners job to react (ie add the message to the document).
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,993
    Rep Power
    5

    Default Re: StreamCorruptedException invalid type code: 00

    What Tolls said. And I stress you to take a step back and go read up more about how streams work.

    For further help - you have to try something yourself first. This forum is about (you) learning first, which means you have to do it. Giving examples right off the bat is counterproductive to that intended result. So don't ask for examples, ask for further clarification if you need it. Me personally I don't usually tell the whole story because I a) am lazy and b) want you to have the chance to show off yourself what you can do (after thinking about it) in stead of being spoon-fed that which you didn't actually need.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Re: StreamCorruptedException invalid type code: 00

    Quote Originally Posted by gimbal2 View Post
    What Tolls said. And I stress you to take a step back and go read up more about how streams work.

    For further help - you have to try something yourself first. This forum is about (you) learning first, which means you have to do it. Giving examples right off the bat is counterproductive to that intended result. So don't ask for examples, ask for further clarification if you need it. Me personally I don't usually tell the whole story because I a) am lazy and b) want you to have the chance to show off yourself what you can do (after thinking about it) in stead of being spoon-fed that which you didn't actually need.
    I didn't ask you to code a full chat application. I have done most of it myself. I just needed to see how the "storage object" looks like. is it making a class or a method? how to synchronize that so multiple threads can read the same object without error. I needed some help or hint and that's all. And I clearly wrote that I didn't ask without trying. I don't like to be spoon-fed, I do my task myself.

    Anyways! my actual question was whether two threads can read the same objectinputstream or not. You have answered. And I appreciate that. Thanks for giving your time to my question.

  7. #7
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Re: StreamCorruptedException invalid type code: 00

    Quote Originally Posted by Tolls View Post
    Have one thread that owns the object input stream.
    It's sole purpose is to read from the stream creating objects.
    It adds these to a queue of incoming messages.
    Other threads read from this queue, via a synchronised method.

    Or have the reading thread prompt a listener when a new object is complete.
    It's the listeners job to react (ie add the message to the document).
    I'll try the first suggestion. Thanks for your answer. I'll post again whether I succeed or not.

  8. #8
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: StreamCorruptedException invalid type code: 00

    can you use a conncurrent hashmap

Similar Threads

  1. Invalid type code 00
    By anoorally in forum New To Java
    Replies: 1
    Last Post: 03-10-2012, 03:13 PM
  2. Replies: 3
    Last Post: 03-08-2012, 08:33 PM
  3. Interface return type invalid
    By Karenthian in forum New To Java
    Replies: 8
    Last Post: 05-09-2011, 11:58 PM
  4. Invalid Method Declaration; Return Type Required
    By bremzb in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2011, 10:12 PM
  5. Replies: 0
    Last Post: 05-17-2010, 02:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •