Results 1 to 3 of 3
- 06-01-2010, 12:21 AM #1
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
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
and setting up object streams:Java Code:socket = new Socket(host, 1337);
I get stuck inside the following blockJava Code:out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream());
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.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!");}
Complete class code:
Complete output: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){} } }
(and nothing after that)Java Code:about to send user thread runs entered while trying hard
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:
where a and u are previously defined.Java Code:ClientConn conn = new ClientConn((Access) a); conn.Start("localhost",(User) u);
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 12:24 AM.
- 06-01-2010, 02:16 AM #2
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 02:18 AM.
- 06-01-2010, 02:58 AM #3
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
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 03:53 AM. Reason: solution doesn't quite work
Similar Threads
-
How many no. of exceptions can be thrown????
By Stephen Douglas in forum New To JavaReplies: 8Last Post: 04-30-2010, 05:12 PM -
ObjectInputStream does not initialize
By Singing Boyo in forum New To JavaReplies: 1Last Post: 06-03-2009, 08:11 AM -
InputStream, Iterator, readObject issues while deserializing multiple objects
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-22-2008, 06:16 PM -
Null pointer Exception. after a bit of execution!! Plz help me
By rohan in forum Java AppletsReplies: 2Last Post: 05-01-2008, 10:14 AM -
Which exception is thrown.....
By money123 in forum New To JavaReplies: 1Last Post: 07-30-2007, 03:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks