Results 1 to 3 of 3
  1. #1
    r00tb33r is offline Member
    Join Date
    Jun 2010
    Posts
    14
    Rep Power
    0

    Exclamation Execution stuck at ObjectInputStream .readObject() with NO Exception thrown.JAVA BUG?

    Hello all,
    I am working on a multiuser client-server application project for college computer science course.
    I consider myself an intermediate java programmer (2 years experience) and decent amount of previous object oriented programming (6 years experience programming in C++).
    THE PROBLEM:
    In my client after connecting the socket
    Java Code:
    socket = new Socket(host, 1337);
    and setting up object streams:
    Java Code:
    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());
    I get stuck inside the following block
    Java Code:
    try{
       System.out.println("trying hard");
       if ((obj = in.readObject()) != null) System.out.println("say hello to my little friend");
    }catch(Exception e){System.out.println("bang!");}
    and judging by the output it gets stuck or freezes inside the IF condition. Object stream contents *should* be null at this point since server is waiting for data from client.

    Complete class code:
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    /**
     *
     * @author Vladislav Sorkin
     */
    public class ClientConn implements Runnable{
            Socket socket = null;
            ObjectOutputStream out = null;
            ObjectInputStream in = null;
            boolean connected;
            Session session;
            User user;
            Object obj;
            Thread t;
            String host;
            LinkedList queue = new LinkedList();
            ClientAccess access;
            ClientProtocol protocol = new ClientProtocol(access);
    
        public ClientConn(ClientAccess a) {
            access=a;
        }
    
        public void in(Object o){
            queue.add(o);
        }
    
        public void Start(String h, User u) throws IOException{
            user=u;
            host=h;
    
            try {
                socket = new Socket(host, 1337);
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());
                t=new Thread(this);
                connected=true;
                t.start();
                System.out.println("about to send user");
                queue.add(user);
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: "+host);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: "+host);
            }
        }
    
        public void run(){
            System.out.println("thread runs");
            try{
            while (connected==true) {
                System.out.println("entered while");
                try{
                    System.out.println("trying hard");
                    [COLOR="Red"][SIZE="4"][B]if ((obj = in.readObject()) != null) System.out.println("say hello to my little friend");//protocol.Process(obj);[/B][/SIZE][/COLOR]
                }catch(Exception e){System.out.println("bang!");}
                
                System.out.println("connection thread running.  queue has items: "+!queue.isEmpty());
    
                if (!queue.isEmpty())
                    out.writeObject(queue.removeFirst());
                    out.flush();
            }
            } catch (Exception e) {
    	    e.printStackTrace();
    	}
            try{
            out.close();
            in.close();
            socket.close();
            }catch(IOException e){}
        }
    
        public void Stop(){
            connected=false;
            try{
            out.close();
            in.close();
            socket.close();
            }catch(IOException e){}
        }
    }
    Complete output:
    Java Code:
    about to send user
    thread runs
    entered while
    trying hard
    (and nothing after that)

    Notes:
    Session and User are serializable classes that I send through the object streams. Client sends a User object which contails login credentials, and server replies with a Session object.
    queue LinkedList is the queue from which the client sends object (we don't even get to reading from it since code stops before that point, so don't even worry about it)
    ClientAccess access enables access to other resources of the client such as its windows. Not used anywhere at this time, so don't worry about it either.
    ClientProtocol protocol performs actions on the client depending on object types and object contents received from the server. It is commented out on the very line in question, so again, its not used by the code with the problem.
    External calls to this class look like this:
    Java Code:
    ClientConn conn = new ClientConn((Access) a);
    conn.Start("localhost",(User) u);
    where a and u are previously defined.

    WHAT I NEED:
    Explanation of why execution just stops and nothing happens and solution to the problem as I don't know how to proceed with checking and reading ObjectInputStream of a working socket connection.
    Help is much appreciated.
    Last edited by r00tb33r; 06-01-2010 at 01:24 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Have you tried the available() method to avoid blocking if there is nothing to read?

    The read() method blocks if there is nothing to read.
    Last edited by Norm; 06-01-2010 at 03:18 AM.

  3. #3
    r00tb33r is offline Member
    Join Date
    Jun 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have you tried the available() method to avoid blocking if there is nothing to read?

    The read() method blocks if there is nothing to read.
    That's precisely it. I didn't know it was a blocking read. I thought it returned a null when its empty. I changed the surrounding code to check if there are available bytes to read and now it avoids blocking if there is nothing in the stream. Everything works like it should now.
    Thank you so much!

    [EDIT]
    Sadly the above solution solves only the immediate problem of blocking, but actually causes another major problem. Method available() for ObjectInputStream *always* returns a zero. It isn't a check of stream contents.
    So by checking available() your program will never ever read the stream.
    I did some more reading on the subject and (correct me if I'm wrong this time) apparently doing a blocking read is the only way to know that there is something in the stream.
    The only workaround I can think of is to add another thread that will take care of writing so communication isn't halted while the blocking read has stopped the thread.

    Personal thoughts: While there might be a good reason why stream status checking isn't implemented many users around the web and I feel that there should be an alternative to an unexpected block. I consider this a bug in the Java library.
    Last edited by r00tb33r; 06-01-2010 at 04:53 AM. Reason: solution doesn't quite work

Similar Threads

  1. How many no. of exceptions can be thrown????
    By Stephen Douglas in forum New To Java
    Replies: 8
    Last Post: 04-30-2010, 06:12 PM
  2. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 09:11 AM
  3. Replies: 0
    Last Post: 12-22-2008, 07:16 PM
  4. Replies: 2
    Last Post: 05-01-2008, 11:14 AM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 04:41 PM

Posting Permissions

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