• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Stream from Socket  RSS feed

 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
I am try to create an Object stream from a socket connection- I can not. The client and server connect- I can get the Input ( and Output stream) and the BufferedStream but I can not get the ObjectStream- both on the client and the server. No timeouts or exceptions are thrown the code just sits there.
Any thing special needs to be done to get the object stream from the socket?
Thanks a bunch
Sanjay
 
Arunagiri, Shanmugham
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay,
Can u post the code here, So that the bug can be identified clearly.
Arun
SCJP
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
The code looks like this:
try {

System.out.println("SocketClient:looking for Socket connection on Host: "+HOST+" Port: "+PORT);
socket = new Socket(HOST, PORT);
System.out.println("SocketClient:got Socket connection");
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
System.out.println("SocketClient:got IO streams");
BufferedInputStream bis = new BufferedInputStream (is);
BufferedOutputStream bos = new BufferedOutputStream (os);
System.out.println("SocketClient:got Buffered IO streams");
//******* the code stops after the above print stmt: No time out //*******or exception is thrown
ois = new ObjectInputStream ( bis);
oos = new ObjectOutputStream ( bos);
System.out.println("SocketClient:got Object IO streams...");
startListener();
System.out.println("SocketClient:Ready...");
} catch (Exception e) {
System.out.println("SocketClient:initSockets:exception: "+e.getMessage());
e.printStackTrace();
throw new Exception("SocketClient:exception: e.getMessage());
}
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello, I have a suggestion for you to solve this problem, in case you haven't figured it out since you last posted. The best way to create a dead lock is to try to read from both ends of a stream at the same time. I assume that the code for your server and your client are both attempting to get the ObjectInputStream before getting the ObjectOutputStream. Basically, all you have to do to fix this problem is put the getObjectOutputStream line BEFORE the getObjectInputStream line..



Hope this helps!



RL

 
Siegfried Steiner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sanjay -
Flushing the object output stream just after construction of the same solves the problem, even if the underlain stream is a buffered stream:
<code snippet>
...
// -----------------------------------------
// Construct the object output stream - i.e.
// get the 'someOutStream' from your socket.
// -----------------------------------------
ObjectOutputStream oos = new ObjectOutputStream(someBufferedOutStream);
// -----------------------------------------
// Flush the stream just after constuction,
// because the stream's header data is to
// be pushed to the waiting corresponding
// input stream ... There seems to be no
// other way to send the stream's header data:
// -----------------------------------------
oos.flush();
...
</code snippet>
Still the order of the streams being created is important -
hope this helps,
Siegfried
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Just to complete this saga-
The ObjectInputStream blocks. So if you do not want to block- you can do .available() on the InputStraem/ BuffredInputStream - for some reason- these calls do not block.
Thanks evrybody,
Sanjay
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!